Creating a timeout

  • Thread starter Thread starter Adam Short
  • Start date Start date
A

Adam Short

I have a webservice which takes a hostname and drive letter as input and
returns the free space for that drive on that host as a long. Sometimes
it will take forever to get information from a recalcitrant host, but it
keeps trying far longer than I want it to. How can I set up a timeout
for the process, so that after a couple of seconds it returns a failure
status and gives up waiting?

Currently, the method maps the drive, finds its available space, and
then unmaps it, which all works quite neatly. If the host doesn't exist
or the drive doesn't exist it returns -1. Otherwise it returns the free
space in bytes as a long. We're currently experiencing network
difficulties because of a relocation. Most of our important business
systems, including DNS servers and the like, are now about 10 miles away
and are accessed via a much, much slower link than we're used to. Some
hosts simply take ages to respond and I'd like to impose a timeout
somehow. I thought maybe some sort of timer based arrangement might
work, but now that I'm trying it I can't actually work out what needs to
be done.

Any help would be appreciated.
 
There is a parameter on the Machine.Config file (or you can put it on your
Web.Config file to affect only your website) called@

httpRuntime, attribute : executionTimeout= (in seconds)


Ex:
<system.web>
<httpRuntime executionTimeout="90" maxRequestLength="4096"
useFullyQualifiedRedirectUrl="false" minFreeThreads="8"
minLocalRequestFreeThreads="4" appRequestQueueLimit="100"
enableVersionHeader="true"/>
<system.web>

This will timeout after 90 seconds

Cheers
 
One idea would be to use different threads. Currently I'm working in
1.1, but I know the 2.0 framework has a read timeout on a socket. To
get that same functionality, I perform the read in a seperate thread,
use a flag to determine if it has completed or if it's still blocking,
and monitor that for x milliseconds. If the time passes and the method
is still blocking I just kill the thread. It's rather complicated and
probably a little dangerous, so I would make sure that you have a firm
understanding of threads in .NET.

If this sounds like something you would like to try and you would like
to see some code let me know. Hope this helps ~ Justin
 
Hi Justin,

It will not work on a webservice environment, if I call a function
asynchronicly on a webservice and you start the reading in another thread the
function will never return anything, what is more, you should never abort a
thread (you can not kill it otherwise), it is considered bad practice and
will raise an exception. In order to do what you want you should use the same
thread where the calling come from and use the Monitor.Enter function with a
explicit timeout, but is not recommended in this scenario.

Hope this helps to enhance the threads experience.
Regards
 
Back
Top