M
Martin
Hi,
I have an application that calls an async delegate from the UI thread.
The async operation throws an exception with an inner exception
attached. When I get the callback in my form class I call
this.Invoke(...) which throws an exception as expected. However, the
exception thrown is the inner exception! Can anyone explain this? I've
attached a complete Program.cs below that reproduces the error.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace TestAsyncException
{
class MyForm : Form
{
public MyForm()
{
this.Load += new EventHandler(MyForm_Load);
}
void MyForm_Load(object sender, EventArgs e)
{
ManualResetEvent theEvent = new ManualResetEvent(false);
VoidMethod voidMethod = WorkerMethod;
IAsyncResult iar = voidMethod.BeginInvoke(
delegate(IAsyncResult result)
{
try
{
VoidMethod dg = (VoidMethod)result.AsyncState;
if (this.InvokeRequired)
this.Invoke(new
AsyncCallback(dg.EndInvoke), new object[] { result });
else
dg.EndInvoke(result);
}
catch (Exception exc)
{
// Displays "Got error: Original Error"!!
Trace.TraceInformation("Got error: " +
exc.Message);
}
theEvent.Set();
}, voidMethod);
// Wait until the async operation is complete
while (!theEvent.WaitOne(10, false))
Application.DoEvents();
}
delegate void VoidMethod();
void WorkerMethod()
{
try
{
ExceptionMethod();
}
catch (Exception e)
{
throw new Exception("New Error", e);
}
}
void ExceptionMethod()
{
throw new Exception("Original Error");
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MyForm());
}
}
}
I have an application that calls an async delegate from the UI thread.
The async operation throws an exception with an inner exception
attached. When I get the callback in my form class I call
this.Invoke(...) which throws an exception as expected. However, the
exception thrown is the inner exception! Can anyone explain this? I've
attached a complete Program.cs below that reproduces the error.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace TestAsyncException
{
class MyForm : Form
{
public MyForm()
{
this.Load += new EventHandler(MyForm_Load);
}
void MyForm_Load(object sender, EventArgs e)
{
ManualResetEvent theEvent = new ManualResetEvent(false);
VoidMethod voidMethod = WorkerMethod;
IAsyncResult iar = voidMethod.BeginInvoke(
delegate(IAsyncResult result)
{
try
{
VoidMethod dg = (VoidMethod)result.AsyncState;
if (this.InvokeRequired)
this.Invoke(new
AsyncCallback(dg.EndInvoke), new object[] { result });
else
dg.EndInvoke(result);
}
catch (Exception exc)
{
// Displays "Got error: Original Error"!!
Trace.TraceInformation("Got error: " +
exc.Message);
}
theEvent.Set();
}, voidMethod);
// Wait until the async operation is complete
while (!theEvent.WaitOne(10, false))
Application.DoEvents();
}
delegate void VoidMethod();
void WorkerMethod()
{
try
{
ExceptionMethod();
}
catch (Exception e)
{
throw new Exception("New Error", e);
}
}
void ExceptionMethod()
{
throw new Exception("Original Error");
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MyForm());
}
}
}