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-- ;
}
}