Reputation: 97
i have a problem where I run part of my code using the Dispatcher
class from Application.Current
namespace. Then I want to construct a follow up operation using the ContinueWith
method of the task returned by Dispatcher.Invoke()
.
In this particular case, the follow up operation needs to be run from within the UI thread aswell, so it needs to be wrapped in the Dispatcher.Invoke
again. This is how I got it to work:
Action doSomeMoreStuff = () => { this.MoreStuff; }
Application.Current.Dispatcher.Invoke(() => this.DoStuff).ContinueWith(x => Application.Current.Dispatcher.Invoke(this.DoSomeMoreStuff));
I want to keep it generic however, and there might be situations where I don't want the follow up code to be run from within the UI thread. So I tried to encapsulate the follow up code itself:
Action doSomeMoreStuff = () => { Application.Current.Dispatcher.Invoke(this.MoreStuff); }
Application.Current.Dispatcher.Invoke(() => this.DoStuff).ContinueWith(x => this.DoSomeMoreStuff);
So as far as I understand this problem, I simply switched the position of the Application.Current.Dispatcher.Invoke()
call. However, the second approach does not work, the code does not get called, and I have no idea why.
What am I not getting here?
Upvotes: 1
Views: 5221
Reputation: 97
I managed to solve my problem, ditching the ContinueWith()
. It works
Action followUpAction = () => { };
Application.Current.Dispatcher.Invoke(
async () =>
{
await this.DoStuff()
followUpAction();
});
Thanks for the helpful comments though.
Upvotes: 1