Threading Timeout Question

G

Guest

Hello!

I have a question that I really can’t think of an answer for, but thought I
might as well give a shot here. I have a web service, created by the VS web
service creator. I have another class, clsWebservice, which provides easy
asynchronous access to each method in the web service. However, the problem
is sometimes a web service call might be dropped, and I want to watch for
that, with a timeout to tell the user that there’s a problem, and ask if he
wants to reissue the request. How would I do the timeout? The easiest answer
would seem to be the AsyncWaitHandle.WaitOne, with a timeout, but that stops
my main thread, so it can’t refresh during that time, and to the user, it
would look like it’s locking up. However, I also couldn’t find a way to start
a thread, and pass parameters through it to a function, to pass to the web
service, which would be ideal – because then it wouldn’t matter if I locked
up that thread.

I apologize; I am rather new at threading.

God bless,

Jose
 
J

Jon Skeet [C# MVP]

JMax said:
I have a question that I really can?t think of an answer for, but thought I
might as well give a shot here. I have a web service, created by the VS web
service creator. I have another class, clsWebservice, which provides easy
asynchronous access to each method in the web service. However, the problem
is sometimes a web service call might be dropped, and I want to watch for
that, with a timeout to tell the user that there?s a problem, and ask if he
wants to reissue the request. How would I do the timeout? The easiest answer
would seem to be the AsyncWaitHandle.WaitOne, with a timeout, but that stops
my main thread, so it can?t refresh during that time, and to the user, it
would look like it?s locking up. However, I also couldn?t find a way to start
a thread, and pass parameters through it to a function, to pass to the web
service, which would be ideal ? because then it wouldn?t matter if I locked
up that thread.

I apologize; I am rather new at threading.

You could start a timer on your main thread, as an option. For how to
effectively pass parameters, see
http://www.pobox.com/~skeet/csharp/threads/parameters.shtml

(You might want to read the rest of the article for general threading
help.)
 
D

Derrick Coetzee [MSFT]

JMax said:
However, the problem is sometimes a web service
call might be dropped, and I want to watch for that, with a timeout
to tell the user that there's a problem, and ask if he wants to
reissue the request. How would I do the timeout? [...] However, I also
couldn't find a way to start a thread, and pass parameters through
it to a function, to pass to the web service [...]

Hi Jose. Probably the best way to do this is using a System.Threading.Timer
object. You can specify a time and a callback function and a threadpool
thread will invoke your callback after the given amount of time has passed.
The TimerCallback delegate takes a state object that allows you to pass data
to the callback, but this isn't strictly necessary; it's always possible to
create a temporary object which holds both the data and the callback. This
is how you can get data to a new thread - just create an object, passing
some data to its constructor, and then call a "Run" or "Start" method on
that object which invokes Thread.Start() on one of its own methods. I hope
this helps.
 

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