all groups > dotnet clr > june 2005 >
You're in the

dotnet clr

group:

Stopping a thread executing a blocking call


Stopping a thread executing a blocking call Netveloper
6/20/2005 11:15:47 AM
dotnet clr:
Hi!

In a previous post I discussed the most suitable approach for
multi-threading in
a provided scenario and was provided very usefull feedback (thanks Stefan
Simek, Jon Skeet and John Conwell).

I am using a WaitHandler.WaitAll to wait for all of my threads to finish
executing,
but I would also like to leverage the timeout feature of this method so that
I wont
end up in a terribly long wait.

On of the last questions I asked was how to shutdown my worker threads if
the
timeout kicked in and Jon pointed me to

http://www.yoda.arachsys.com/csharp/threads/shutdown.shtml

At first I thought this would be the way to go, but when I saw that Jons
code was
based on multiple items being processed from a worker thread, because of the
while-loop in his skeleton code.

In my case I will only have one data collection taking place, for example a
call to
a database or a webservice. Now calling a webservice is a synchronous call,
i.e
blocking call (if I don't use the async begin/endxxx methods), thus the
worker thread
will be blocking executing inside that thread until the webservice responds.
Now
what happens if I call the webservice from the workerthread.. it takes ages
to
respond and in the meantime the timeout kicks in from the mainthreads and
abandons
the wait.. now how do I gracefully shutdown the worker threads in that case?

Should I use the async calls to the webservice and leverage a pattern like
the one Jon
illustrates in his article or is there perhaps another way? The same could
be said for
database access.. it could take time, even timeout and I would be left with
a worker
thread hanging around...The database calls can't be done async (since this
isn't a 2.0
application ;) ..

<insert suggestions / feedback here>

=)

Thanks!

Re: Stopping a thread executing a blocking call Jon Skeet [C# MVP]
6/21/2005 8:48:57 PM
[quoted text, click to view]

<snip>

[quoted text, click to view]

Just to say I *will* be answering this post, as soon as I get time to
do it justice. Don't think I've abandoned you :)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: Stopping a thread executing a blocking call Netveloper
6/22/2005 10:42:44 AM

"Jon Skeet [C# MVP]" <skeet@pobox.com> skrev i meddelandet
news:MPG.1d227f86a41f473498c375@msnews.microsoft.com...
[quoted text, click to view]

*phew* Nice of you to let me know =) Looking forward to some
insight into this =P Amazing how much programming you can do
over the years and manage to stay away from the "dark side" of
multihreading ;)

[quoted text, click to view]

Re: Stopping a thread executing a blocking call Jon Skeet [C# MVP]
6/26/2005 12:00:00 AM
[quoted text, click to view]

<snip>

[quoted text, click to view]

Well, why do you *need* to? If they're not doing anything, I'd just let
them hang around. If the app should stop at that point, make them
background threads. If the app needs to keep going, you could set a
variable to say that you're no longer interested in the results of the
webservice call, and make the webservice thread test that after the
call, and act accordingly.

For database access, you may be able to cancel the request depending on
what database you're using etc. (See things like SqlCommand.Cancel.)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
AddThis Social Bookmark Button