Pietje,
You have to go about this the opposite way actually. The first thing
you need to do is indicate somewhere if the operation completed or not. You
can use a boolean flag for this. Once you have that, you should expose it
as a property (public or private, it is up to you). You need to place a
lock section around the access of this variable.
After that, in your method, you call your operation asynchronously in
another thread (through the thread pool or a separate thread). In the code
that is performing the work, when complete, set the flag to true.
In the code that calls out to the other thread, after the asynchronous
call is made, you can call the static Sleep method to sleep for the amount
of time you want to wait. If the flag is not set when execution resumes,
you can throw your exception.
Of course, this means that if your operation completes, you have to wait
the same amount of time. In order to get around this, replace the boolean
flag with a WaitHandle, such as an event. In your operation, instead of
setting the boolean flag, set the Event. Also, instead of having the thread
put to sleep, you wait on the handle. Before you wait on the handle, you
set your timer to call back a method and set the event when it calls back.
The flag comes in useful here in the timer method, where you will set it to
true. This way, when the call to WaitOne on the Event is complete, you will
know if it timed out or not.
Hope this helps.
"Pietje de kort" <wo******@hotmail.com> wrote in message
news:e0*************************@posting.google.co m...
Hello all,
I want to implement Timeout behaviour in a class of mine. When a
method is called it should timeout after a few seconds. To do this
I've built a
System.Threading.Timer that calls a delegate method after x milli's,
the called
delegate throws an exception indicating timeout.
The big problem is my code will not catch the thrown exception because
it
is generated outside of normal program control. Is there any way to
catch the
exception thrown (other than hooking the UncaughtException event)?
Best regards Wouter
using System.Threading;
public class Test
{
public Test(){};
public DoTimeoutAction()
{
Timer timer = new Timer(new TimerCallback(Timedout),null,
1000,Timeout.Infinite);
Console.WriteLine("Doing something");
Thread.Sleep(5000); // will cause timeout to occur
Console.WriteLine("Done doing something");
timer.Dispose(); // stop timer
}
private void Timedout(object state)
{
throw new ApplicationException("Timeout occured.");
}
static void Main()
{
Test t = new Test();
try
{
t.DoTimeoutAction();
}catch(ApplicationException e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Done!");
Console.ReadLine();
}
}