Is This Safe? Static Class Reference In Static Function

A

Amy L.

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).ReturnValue =
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-- ;
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

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
 
A

Amy L.

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 ) said:
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. said:
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).ReturnValue =
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-- ;
}
}
 
M

Michael S

Amy L. said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top