EndInvoke exception handling...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I can't figure out why, in the following code, the exception is not caught
when EndIncoke is called in the callback handling method... any ideas?

Thanks in advance.
-Andrew



<snip>

static void DoIt()
{
Console.WriteLine("worker thread");
Thread.Sleep(1000);
Console.WriteLine("about to throw...");
Thread.Sleep(1000);
throw new Exception("EXCEPTION MSG");
}

static void Callback(IAsyncResult result)
{
MyDelegate d = (MyDelegate)result.AsyncState;
try
{
d.EndInvoke(result);
}
catch(Exception x)
{
Console.WriteLine(x.Message + " caught! ! !!");
}
}

static void Main(string[] args)
{
MethodInvoker del = DoIt;
del.BeginInvoke(new AsyncCallback(Callback), del);
Thread.CurrentThread.Join();
}


</snip>
 
Hello, Andrew!

When you issue BeginInvoke the method you specified runs on the separate thread. You're handling exception in the completion callback and not in the executing method,
Why is this so?
Well, the reasons can be that your method can complete synchronously.
Or method is completed before thread from thread pool called your callback

Your code will work only when Dolt method takes rather great amount of time ( you manage to block on EndInvoke)
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Back
Top