2

我注意到从我的主应用程序线程以外的线程抛出的异常不会被 DispatcherUnhandledException 事件处理程序捕获。所以我必须像这样手动抛出它们:

Task.Factory.StartNew(() =>
{
    throw new Exception("oops! something went wrong...");

}).ContinueWith((task) =>
{
    if (task.IsFaulted)
    {
        App.Current.Dispatcher.Invoke(new Action(() =>
        {
            throw task.Exception.InnerExceptions.First();
        }));
    }
});

但是,我不想将上述 ContinueWith 方法添加到我创建的每个任务中。我宁愿有一些方法可以自动处理。

4

1 回答 1

2

下面的类解决了这个问题:

/// <summary>
/// Extends the System.Threading.Tasks.Task by automatically throwing the first exception to the main application thread.
/// </summary>
public class TaskEx
{
    public Task Task { get; private set; }

    private TaskEx(Action action)
    {
        Task = Task.Factory.StartNew(action).ContinueWith((task) =>
        {
            ThrowTaskException(task);
        });
    }

    public static TaskEx StartNew(Action action)
    {
        if (action == null)
        {
            throw new ArgumentNullException();
        }

        return new TaskEx(action);
    }

    public TaskEx ContinueWith(Action<Task> continuationAction)
    {
        if (continuationAction == null)
        {
            throw new ArgumentNullException();
        }

        Task = Task.ContinueWith(continuationAction).ContinueWith((task) =>
        {
            ThrowTaskException(task);
        });

        return this;
    }

    private void ThrowTaskException(Task task)
    {
        if (task.IsFaulted)
        {
            App.Current.Dispatcher.Invoke(new Action(() =>
            {
                throw task.Exception.InnerExceptions.First();
            }));
        }
    }
}

现在我可以简单地使用以下代码(与 Task 类完全相同):

TaskEx.StartNew(() =>
{
    // do something that may cause an exception
}).ContinueWith((task) =>
{
    // then do something else that may cause an exception
}).ContinueWith((task) =>
{
    // then do yet something else that may cause an exception
});

然而,与 Task 类不同的是,从这些线程之一抛出的任何异常都将被我的 DispatcherUnhandledException 事件处理程序自动捕获。

于 2012-09-28T00:22:10.900 回答