I thought that if I wrap the EndInvoke call with a try catch if an error is thrown then my catch block would handle it? I must being doing something wrong??? Has to be user error, just not sure what?
EDIT:
I get the “Exception was unhandled by user code” being thrown when I run this which is stopping the application. If I step through the code I see that and then it will go to the catch block. But, I would expect the catch block to handle this and not see the unhandled exception that is stopping the application?
Any suggestions appreciated.
class Program
{
static void Main(string[] args)
{
Action myMethod = new Action(Program.FooOneSecond);
Go("Go Method");
IAsyncResult tag =
myMethod.BeginInvoke(null, "passing some state");
try
{
myMethod.EndInvoke(tag);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
string strState = (string)tag.AsyncState;
Console.WriteLine("State When Calling EndInvoke: "
+ tag.AsyncState.ToString());
Console.Read();
}
static int Work(string s) { return s.Length; throw null; }
static void Go(string s)
{
Console.WriteLine(s);
}
static void FooOneSecond()
{
// sleep for one second!
Thread.Sleep(1000);
// throw an exception
throw new Exception("Exception from FooOneSecond");
}
}
I just ran your code and the exception gets caught every time…