.net Application.Current.Dispatcher.BeginInvoke-在何处放置Try&;接住?
如果我有一个调度器在后台这样调用:.net Application.Current.Dispatcher.BeginInvoke-在何处放置Try&;接住?,.net,wpf,try-catch,.net,Wpf,Try Catch,如果我有一个调度器在后台这样调用: Application.Current.Dispatcher.BeginInvoke(new Action(() => MethodToCall()), DispatcherPriority.Background); Application.Current.Dispatcher.BeginInvoke(() => { try { MethodToCall(); } catch { //handle } ), DispatcherPr
Application.Current.Dispatcher.BeginInvoke(new Action(() => MethodToCall()), DispatcherPriority.Background);
Application.Current.Dispatcher.BeginInvoke(() => {
try
{
MethodToCall();
}
catch
{
//handle
}
), DispatcherPriority.Background);
我应该将上面的代码包装在Try&catch中,还是将Try&catch放在MethodToCall()
方法中
非常感谢,如果您确实有捕获特定异常的理由,那么
try{}catch
应该放在MethodToCall
中如果您确实有捕获特定异常的理由,那么try{}catch
应放在MethodToCall
中,Hi BeginInvoke将在另一个堆栈中执行您的方法。
因此,“Application.Current.Dispatcher.BeginInvoke”的尝试性解决方案将不起作用
您需要这样做:
Application.Current.Dispatcher.BeginInvoke(new Action(() => MethodToCall()), DispatcherPriority.Background);
Application.Current.Dispatcher.BeginInvoke(() => {
try
{
MethodToCall();
}
catch
{
//handle
}
), DispatcherPriority.Background);
或者简单地说是“MethodToCall”
正如ChrisF所述。Hi BeginInvoke将在anoster堆栈中执行您的方法。 因此,“Application.Current.Dispatcher.BeginInvoke”的尝试性解决方案将不起作用 您需要这样做:
Application.Current.Dispatcher.BeginInvoke(new Action(() => MethodToCall()), DispatcherPriority.Background);
Application.Current.Dispatcher.BeginInvoke(() => {
try
{
MethodToCall();
}
catch
{
//handle
}
), DispatcherPriority.Background);
或者简单地说是“MethodToCall”
正如克里斯夫所说