Reputation: 47
My hope is that test.BeginInvoke() throws an TimeoutException after the time limit. but, It doesn't catch TimeoutException. How can I fix it? Thank you so much.
public delegate void AasyncDelegateCaller(int callDuration, out int threadId);
public void AasyncDelegate(int callDuration, out int threadId)
{
Thread.Sleep(callDuration);
threadId = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("TimeoutException");
throw new TimeoutException();
}
private static void Search()
{
try
{
int threadId;
Program p = new Program();
AasyncDelegateCaller caller = new AasyncDelegateCaller(p.AasyncDelegate);
IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);
SearchProgram(); // Sometimes it takes a long time. So I want to throw TimeoutException at that time
}
catch (TimeoutException)
{
Console.WriteLine("It takes Long! Bye");
}
}
Upvotes: 0
Views: 55