WPF - 使用BackgroundWorker进行异步命令的错误处理

发布于 2024-09-28 01:19:55 字数 1870 浏览 6 评论 0原文

我正在使用 这篇博文,但我不知道如何处理我的 action 委托中发生的异常。我创建了以下命令:

new AsyncDelegateCommand(
    readFile, // action to perform
    () => shouldReadFile, // can the action be executed?
    obj => readFileFinished(true), // run after action successfully completed
    ex => readFileFinished(false) // run after action failed
)

我希望捕获 readFile 方法中抛出的任何异常,以及 RunWorkerCompletedEventArgs 设置,以便我在 BackgroundWorker.RunWorkerCompleted 处理程序中访问。但是,当我运行应用程序时,我因 readFile 抛出的异常之一而停止,因此我从未将其发送到 RunWorkerCompleted 的处理程序。这是我的 AsyncDelegateCommand 构造函数:

public AsyncDelegateCommand(Action action, Func<bool> canExecute,
    Action<object> completed, Action<Exception> error)
{
    if (null == action)
    {
        throw new ArgumentNullException("action");
    }
    _worker.DoWork += (sender, e) =>
    {
        CommandManager.InvalidateRequerySuggested();
        // This can possibly throw an exception:
        action();
    };
    _worker.RunWorkerCompleted += (sender, e) =>
    {
        if (null != completed && null == e.Error)
        {
            completed(e.Result);
        }
        // I never make it here:
        if (null != error && null != e.Error)
        {
            error(e.Error);
        }
        CommandManager.InvalidateRequerySuggested();
    };
    _canExecute = canExecute;
}

我想要的是 action 抛出的任何异常都被捕获并填充到 Error 中以便在 中处理RunWorkerCompleted。我该如何实现这一目标?

I'm using the AsyncDelegateCommand class from this blog post but I'm having trouble knowing what to do with exceptions that occur in my action delegate. I create the following command:

new AsyncDelegateCommand(
    readFile, // action to perform
    () => shouldReadFile, // can the action be executed?
    obj => readFileFinished(true), // run after action successfully completed
    ex => readFileFinished(false) // run after action failed
)

I was expecting any exceptions thrown in my readFile method to get caught and for the Error property of RunWorkerCompletedEventArgs to be set, for me to access in my BackgroundWorker.RunWorkerCompleted handler. However, when I run my app, I get stopped on one of the exceptions that readFile throws, so I never make it to my handler for RunWorkerCompleted. Here's my AsyncDelegateCommand constructor:

public AsyncDelegateCommand(Action action, Func<bool> canExecute,
    Action<object> completed, Action<Exception> error)
{
    if (null == action)
    {
        throw new ArgumentNullException("action");
    }
    _worker.DoWork += (sender, e) =>
    {
        CommandManager.InvalidateRequerySuggested();
        // This can possibly throw an exception:
        action();
    };
    _worker.RunWorkerCompleted += (sender, e) =>
    {
        if (null != completed && null == e.Error)
        {
            completed(e.Result);
        }
        // I never make it here:
        if (null != error && null != e.Error)
        {
            error(e.Error);
        }
        CommandManager.InvalidateRequerySuggested();
    };
    _canExecute = canExecute;
}

What I would like is for any exception that action throws to be caught and stuffed into Error for handling in RunWorkerCompleted. How do I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像极了他 2024-10-05 01:19:55

啊,没关系,这是 Visual Studio 为我做的事情:

如果操作引发代码无法处理的异常,BackgroundWorker 会捕获该异常并将其传递到 RunWorkerCompleted 事件处理程序,在该处理程序中该异常将作为 System.ComponentModel.RunWorkerCompletedEventArgs 的 Error 属性公开。如果您在 Visual Studio 调试器下运行,则调试器将在 DoWork 事件处理程序中引发未处理异常的位置处中断。

-- BackgroundWorker.DoWork 事件

我正在运行我的应用程序在 VS 中带有调试器。

Arrgh, never mind, it's Visual Studio doing stuff for me:

If the operation raises an exception that your code does not handle, the BackgroundWorker catches the exception and passes it into the RunWorkerCompleted event handler, where it is exposed as the Error property of System.ComponentModel.RunWorkerCompletedEventArgs. If you are running under the Visual Studio debugger, the debugger will break at the point in the DoWork event handler where the unhandled exception was raised.

-- BackgroundWorker.DoWork Event

I was running my app with the debugger in VS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文