473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Is This Safe? Static Class Reference In Static Function

I have a method below called "ResolveHostname" which is called from the
ThreadPool.QueueUserWorkItem. My concern is with the static dnsClient class
where I am calling dnsClient.Lookup( Hostname) in this method. This method
can be executing at multiple times, but on different threads. All the
dnsClient.Lookup method does is resolve a hostname to an IP Address. It
does not modify any other variables or anything. The reason why I made
dnsClient a static class instance is that its instantiation is very
expensive. With 10 running threads and instantiating dnsClient in the
"ResolveHostname" function it takes over 6 seconds for my app to run. When
I use a static class instance the time is decreased to 2 seconds.

So my question is - is what I am doing a recipe for disaster? Or since they
are on different threads I should not have any conflicts since it may be in
different memory space?

Thanks
Amy

public static int tCounter ;
public static object myMethodLock = new object();
public static DnsClient dnsClient = new DnsClient() ;

static void ResolveHostname( Object hostNameLookupData )
{
String Hostname = ((HostNameLookupData)hostNameLookupData).Hostname ;

Console.WriteLine("Entered Resolve At: " + System.DateTime.Now ) ;

try
{
System.Net.IPAddress[] ipAddress = dnsClient.Lookup( Hostname ) ;
Console.WriteLine( System.DateTime.Now.ToString( "yyyy-MM-dd
HH:mm:ss.fff" ) + " Resolved " + Hostname + " to " +
ipAddress[0].ToString() ) ;
((HostNameLookupData)hostNameLookupData).ReturnVal ue =
ipAddress[0].ToString() ;

}
catch ( DnsException )
{
Console.WriteLine( System.DateTime.Now.ToString( "yyyy-MM-dd
HH:mm:ss.fff" ) + " Unable To Resolve " + Hostname ) ;
}

lock( myMethodLock )
{
tCounter-- ;
}
}
Nov 16 '05 #1
3 3053
hi amy

Why are you using a dnsClient class instead of using the Dns class provided
by the framework?

if for some reason you need doing so, then as long as Lookup does not
access/change instances variables then you should be ok with your current
scenario.

still, you better check the dns.resolve() method.

cheer,s
i

"Amy L." <am**@paxemail.com> wrote in message
news:Os**************@TK2MSFTNGP10.phx.gbl...
I have a method below called "ResolveHostname" which is called from the
ThreadPool.QueueUserWorkItem. My concern is with the static dnsClient
class
where I am calling dnsClient.Lookup( Hostname) in this method. This
method
can be executing at multiple times, but on different threads. All the
dnsClient.Lookup method does is resolve a hostname to an IP Address. It
does not modify any other variables or anything. The reason why I made
dnsClient a static class instance is that its instantiation is very
expensive. With 10 running threads and instantiating dnsClient in the
"ResolveHostname" function it takes over 6 seconds for my app to run.
When
I use a static class instance the time is decreased to 2 seconds.

So my question is - is what I am doing a recipe for disaster? Or since
they
are on different threads I should not have any conflicts since it may be
in
different memory space?

Thanks
Amy

public static int tCounter ;
public static object myMethodLock = new object();
public static DnsClient dnsClient = new DnsClient() ;

static void ResolveHostname( Object hostNameLookupData )
{
String Hostname = ((HostNameLookupData)hostNameLookupData).Hostname ;

Console.WriteLine("Entered Resolve At: " + System.DateTime.Now ) ;

try
{
System.Net.IPAddress[] ipAddress = dnsClient.Lookup( Hostname ) ;
Console.WriteLine( System.DateTime.Now.ToString( "yyyy-MM-dd
HH:mm:ss.fff" ) + " Resolved " + Hostname + " to " +
ipAddress[0].ToString() ) ;
((HostNameLookupData)hostNameLookupData).ReturnVal ue =
ipAddress[0].ToString() ;

}
catch ( DnsException )
{
Console.WriteLine( System.DateTime.Now.ToString( "yyyy-MM-dd
HH:mm:ss.fff" ) + " Unable To Resolve " + Hostname ) ;
}

lock( myMethodLock )
{
tCounter-- ;
}
}

Nov 16 '05 #2
Thank you for your response. I am using the ComponetSpace.Dns library
because the .Net DNS implementation when using "Resolve" only returns 1 IP
address even if multiple IP addresses are associated with a hostname. Also,
when doing reverse DNS lookup's the .Net DNS library will do a Netbios query
if the IP address does not have a PTR record which is undesirable in my
application. Plus I am also going to need support for processing different
types of records like TXT, NS, and SOA which the framework does not support
at this time. Due to those issues I had to offload those kinds of requests
to a 3rd Party Lib.

Again, thank you so much for looking at my issue I appreciate it.
Amy

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2****************@TK2MSFTNGP12.phx.gbl...
hi amy

Why are you using a dnsClient class instead of using the Dns class provided by the framework?

if for some reason you need doing so, then as long as Lookup does not
access/change instances variables then you should be ok with your current
scenario.

still, you better check the dns.resolve() method.

cheer,s
i

"Amy L." <am**@paxemail.com> wrote in message
news:Os**************@TK2MSFTNGP10.phx.gbl...
I have a method below called "ResolveHostname" which is called from the
ThreadPool.QueueUserWorkItem. My concern is with the static dnsClient
class
where I am calling dnsClient.Lookup( Hostname) in this method. This
method
can be executing at multiple times, but on different threads. All the
dnsClient.Lookup method does is resolve a hostname to an IP Address. It
does not modify any other variables or anything. The reason why I made
dnsClient a static class instance is that its instantiation is very
expensive. With 10 running threads and instantiating dnsClient in the
"ResolveHostname" function it takes over 6 seconds for my app to run.
When
I use a static class instance the time is decreased to 2 seconds.

So my question is - is what I am doing a recipe for disaster? Or since
they
are on different threads I should not have any conflicts since it may be
in
different memory space?

Thanks
Amy

public static int tCounter ;
public static object myMethodLock = new object();
public static DnsClient dnsClient = new DnsClient() ;

static void ResolveHostname( Object hostNameLookupData )
{
String Hostname = ((HostNameLookupData)hostNameLookupData).Hostname ;

Console.WriteLine("Entered Resolve At: " + System.DateTime.Now ) ;

try
{
System.Net.IPAddress[] ipAddress = dnsClient.Lookup( Hostname ) ;
Console.WriteLine( System.DateTime.Now.ToString( "yyyy-MM-dd
HH:mm:ss.fff" ) + " Resolved " + Hostname + " to " +
ipAddress[0].ToString() ) ;
((HostNameLookupData)hostNameLookupData).ReturnVal ue =
ipAddress[0].ToString() ;

}
catch ( DnsException )
{
Console.WriteLine( System.DateTime.Now.ToString( "yyyy-MM-dd
HH:mm:ss.fff" ) + " Unable To Resolve " + Hostname ) ;
}

lock( myMethodLock )
{
tCounter-- ;
}
}


Nov 16 '05 #3

"Amy L." <am**@paxemail.com> wrote in message
news:Os**************@TK2MSFTNGP10.phx.gbl...
dnsClient.Lookup method does is resolve a hostname to an IP Address.


Well, my two cents is that if the docs don't clearly specify that it is
thread-safe, I would treat it as non-threadsafe and lock the thing up.

Thread bugs are the worst bugs there is and I've done way too much
deadlock-hunting in my lie to ever again assume things will work because it
ought. *s*

Also, the DNS-server will probably queue your requests anyways, as it has a
cache, so why not be nice and queue your request for it and let other
application be served?

- Michael S
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jean-Francois Brault | last post by:
I wrote a crappy class for radian angle management. The class consists of an array of radian values. I put all these things in a class in which all methods are static, so I can access it anywhere...
7
by: Jim Showalter | last post by:
I always thought that it is safe for a function to return a pointer to static storage. And the following code does compile quietly with: gcc -pedantic -Wall -o foo foo.c #include <stdio.h> ...
1
by: JezB | last post by:
I have a static class that is instantiated once on Application_Start in global.asax :- protected void Application_Start(Object sender, EventArgs e) { int dialectID = 1;...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: Senna | last post by:
Hi I have some questions about this code, http://code.doria.se/?c=18. 1. Will the Adapter class be thread-safe? 2. In the #region Way 1 I use lock(_lockobj) every time I go about and do...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
7
by: reyesflaco | last post by:
I am developing an application using asp.net 2.0. I created all my business objects in my app_code folder. As of right now, all my classes are public. In my aspx pages, I am declaring the class...
2
by: reyesflaco | last post by:
I am developing an application using asp.net 2.0. I created all my business objects in my app_code folder. As of right now, all my classes are public. In my aspx pages, I am declaring the class...
3
by: andreas.zetterstrom | last post by:
I'm implementing some different c++ classes which I want to be thread safe. All these classes contain lists or arrays of some kind. I'm using protected sections to make them thread safe. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.