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

时间:2022-11-29 20:19:02
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 个解决方案

#1


0  

Is this a windows forms app?

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

Make sure your subscribed to the threadexception event.

确保您订阅了threadexception事件。

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

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

#1


0  

Is this a windows forms app?

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

Make sure your subscribed to the threadexception event.

确保您订阅了threadexception事件。

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

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