Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net Application.Current.Dispatcher.BeginInvoke-在何处放置Try&;接住?_.net_Wpf_Try Catch - Fatal编程技术网

.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”

正如克里斯夫所说