WPF - 使用BackgroundWorker进行异步命令的错误处理
我正在使用 这篇博文,但我不知道如何处理我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊,没关系,这是 Visual Studio 为我做的事情:
-- BackgroundWorker.DoWork 事件
我正在运行我的应用程序在 VS 中带有调试器。
Arrgh, never mind, it's Visual Studio doing stuff for me:
-- BackgroundWorker.DoWork Event
I was running my app with the debugger in VS.