Houman
Houman

Reputation: 66320

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

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,

Upvotes: 1

Views: 5495

Answers (2)

mo.
mo.

Reputation: 3534

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.

Upvotes: 3

ChrisF
ChrisF

Reputation: 137128

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

Upvotes: 4

Related Questions