BeginInvoke - 当try块中的方法抛出时,为什么此代码中的catch块没有捕获到InvalidOperationException异常?

时间:2011-11-29 13:23:39

标签: c# .net asynchronous delegates

private void f()
{
    Action<string> action = WriteLogToFile;
    AsyncCallback callBack = LogCallBack;

    action.BeginInvoke("Hello world!", callBack, action);
}

private void WriteLogToFile(string message)
{
    Monitor.Enter(locker);

    string formattedLog = Write(message);

    try
    {
        SendEmailNotifications(formattedLog);
    }
    catch (InvalidOperationException)
    {
    ...
    }

    Monitor.Exit(locker);
}

private static void LogCallBack(IAsyncResult r)
{
    if (r.IsCompleted)
    {
        ((Action<String>) r.AsyncState).EndInvoke(r);
    }
}

1 个答案:

答案 0 :(得分:0)

这是一个Windows窗体应用程序吗?

确保您订阅了threadexception事件。

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx