【发布时间】:2012-03-06 14:51:45
【问题描述】:
如果我有一个调度程序在后台像这样调用:
Application.Current.Dispatcher.BeginInvoke(new Action(() => MethodToCall()), DispatcherPriority.Background);
我应该将上面的代码包装在 Try & catch 中还是将 try & catch 放在 MethodToCall() 方法中?
非常感谢,
【问题讨论】:
如果我有一个调度程序在后台像这样调用:
Application.Current.Dispatcher.BeginInvoke(new Action(() => MethodToCall()), DispatcherPriority.Background);
我应该将上面的代码包装在 Try & catch 中还是将 try & catch 放在 MethodToCall() 方法中?
非常感谢,
【问题讨论】:
如果您确实需要捕获特定异常,则应将try { } catch 放在MethodToCall 中。
【讨论】:
您好 BeginInvoke 将在 anoster Stack 中执行您的方法。 因此,围绕“Application.Current.Dispatcher.BeginInvoke”的尝试将不起作用。
你需要做这样的事情:
Application.Current.Dispatcher.BeginInvoke(() => {
try
{
MethodToCall();
}
catch
{
//handle
}
), DispatcherPriority.Background);
或者只是在“MethodToCall”中。
正如 ChrisF 所说。
【讨论】: