Application.Current.Dispatcher.BeginInvoke - Where to place Try & catch?

非 Y 不嫁゛ 提交于 2020-01-15 11:26:10

问题


if I have a dispatcher invoking in background like this:

Application.Current.Dispatcher.BeginInvoke(new Action(() => MethodToCall()), DispatcherPriority.Background);

Should I have wrap the code above inside a Try & catch or place the try & catch inside the MethodToCall() method?

Many Thanks,


回答1:


If you really have a case for catching a specific exception then the try { } catch should be placed inside MethodToCall.




回答2:


Hi BeginInvoke will Execute your Method in anoster Stack. So a try-catch around "Application.Current.Dispatcher.BeginInvoke" will not work.

You need to do something like this:

Application.Current.Dispatcher.BeginInvoke(() => {
try
{
    MethodToCall();
}
catch
{
   //handle
}
), DispatcherPriority.Background);

or simply in "MethodToCall".

As ChrisF stated.



来源:https://stackoverflow.com/questions/9586006/application-current-dispatcher-begininvoke-where-to-place-try-catch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!