EndInvoke exception handling...

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>
 
V

Vadym Stetsyak

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
 

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