code takes a while to return if error

  • Thread starter Thread starter Scot
  • Start date Start date
S

Scot

I have the following code:

Dim IP, domainName As String
domainName = "yahoo.com"

Try
IP = System.Net.Dns.GetHostByName(domainName).AddressList(0).ToString()
Catch ex As Exception
IP = "Not Valid"
End Try

Console.WriteLine(IP)
System.Threading.Thread.Sleep(2000)


When it has a resolvable domain, like yahoo.com it responds back instantly
with the ip address. However, if there is a domain in there that does not
exist, like blue2343.com, it takes upwards of 5 seconds to print "Not Valid"
to the screen. Why is that? and is there a way around it?

Thanks,

Scott
 
Scot,

This is mostly likely due to the time it is taking for your DNS server to
attempt to resolve the DNS name to an IP address. It is not uncommon for
3-6 seconds to pass while DNS attempts to resolve an entry that does not
exist. There is no way around the time it takes for a DNS server to exhaust
its resources in an attempt to resolve hostnames to ip addresses.

HTH,

Raymond Lewallen
 
Hi Raymond

I have no doubt that you are correct, however, I had assumed that Scot was
referring to the time it takes to catch and throw an exception.

I have noticed that the first time an exception is thrown and caught, a .NET
application can take a significant time (several seconds) before returning
control to the catch block.

Unfortunately for Scot, I don't know why this should be, so I am watching
this thread with interest in case there is anything I can do about it as
well. It does not seem to matter whether the exception is thrown manually
either; the first time always takes several seconds.

Charles
 
Charles, Scot,

Perhaps Scot can step through his code and tell us if the delay is coming
when he steps into the GetHostByName method, or when he steps into the
Catch. Personally, I haven't noticed any delays in .NET code when an
exception is being thrown for the first time. Maybe thats because I'm not
looking for it :)

Raymond Lewallen
 
I think Charles is correct in his assumption. I understand that DNS lookups
on non-existant domains take longer, but not in the 3 to 5 second range. A
second or two would be acceptable, but even that would be long if you there
was minimal network latency.

I believe it is something in the Try..Catch segment. Anyone?
 
Scot,

What happens when you step into the code? Does it takes this long everytime
you run the app, even if the process is not recycled? Charles is saying "I
have noticed that the first time an exception is thrown and caught, a .NET
application can take a significant time (several seconds) before returning
control to the catch block." Have you confirmed this is what is happening
by stepping into the try...catch block?

Raymond Lewallen
 
I haven't tried that. Unfortunately I am not an "official" programmer, so I
more or less hack things together until they work. I wouldn't have even the
first bit of a clue as far as how to step into the try....catch block. In
fact this is the first time I have ever used one in vb.net, and it was only
because I couldn't solve the situation another way.

It takes this long everytime I run the app, but it is started from scratch
each time.
 
Scot, set a breakpoint on your GetHostByName line (right-click on the line
and click set breakpoint). When you run the app it will stop there, and
then hit F11 to step into the code line by line and you will see where the
hic-cup is occurring.
 
I set the breakpoint on that line, then hit F11.

It highlights Sub Main() in yellow with a yellow arrow to the left.
F11 again, domainName line is now in yellow
F11, Try in yello
F11, breakpoint line in yellow
F11, End Try in yellow
F11, console.writeline in yellow
F11, sleep in yellow
then End Sub ends up in yellow

Nothing seems to take the 5 seconds, even when I rapidly go through it with
F11.
 
Scot said:
I haven't tried that. Unfortunately I am not an "official" programmer,

Ha! don't worry about being an official programmer. I've met "official" or
"professional" programmers that don't know a damn thing. =)
 
Scot,

Not the first time I've seen something like this. I've actually had code
that would throw exceptions when just running through it, but when I stepped
through it, would work just fine! It stinks to say it, but rebooting often
helped that problem (not that it actually occurred on a regular basis). I'm
afraid I have no more advice on the subject, and I hate to leave you with
"Maybe you'll have to live with it", so hopefully someone else will have a
good suggestion.

Sorry I couldn't be of more help,

Raymond Lewallen
 
Back
Top