2

How can i execute the UpdateTasklist() Method after the SubmitWorkitem() Method without blocking the thread?

private async void SubmitWorkitem(Workitem workitem)
{
    await Task.Run(() => this.SubmitWorkitem(workitem));

    //UpdateTasklist() should be executed after SubmitWorkitem() method.
    //How can i achieve this without blocking the UI thread?
    var locator = new ViewModelLocator();
    locator.Task.UpdateTasklist();
}

EDIT:

The UpdateTasklist() method connects to an wcf webservice and asks for all open workitems. The workitem which is submitted in the SubmitWorkitem() Method is still part of the reply. I thought that would be because UpdateTasklist() is executed before the submission of the workitem is done.

Note that UpdateTasklist() is also an async method

Joel
  • 4,862
  • 7
  • 46
  • 71

1 Answers1

5

Important: DO NOT WRITE ASYNC VOID METHODS (unless you are writing an event-handler)

For the rest:

That is already what happens in your code; this is what await means; basically, your DifferentClass.UpdateTasklist(); method happens as part of the continuation that gets invoked when and only when the first task (this.SubmitWorkitem(workitem)) completes.

With your edit, there is a missing step: you should await the second method, otherwise the method cannot report completion / failure (IIRC the compiler will also nag you):

private async Task SubmitWorkitem(Workitem workitem)
{
    await Task.Run(() => this.SubmitWorkitem(workitem));
    var locator = new ViewModelLocator();
    await locator.Task.UpdateTasklist();
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • the method has to be void because of this: this.SubmitCommand = new RelayCommand(this.SubmitWorkitem, this.CanSubmit); – Joel Jun 18 '13 at 10:06
  • 3
    @Joel hmmm, I guess that has to come under the same bracket as event-handlers then; perhaps the correct guidance is "do not write an async void method unless you have no alternative" - however, keep in mind that the runner here will have *no clue* whether your method has finished yet - as far as the framework is concerned, the method will be "over" at the point it reaches the first `await` (if we assume that the awaited operation doesn't actually finish synchronously, which can happen, but which sounds unlikely here). The point being ... perhaps expect trouble – Marc Gravell Jun 18 '13 at 10:09
  • 2
    @Joel: You do need an `async void` `ICommand.Execute` implementation in this case, but you can "hide" it behind an `async Task` method. This gives you a better API which you can consume directly, e.g., when unit testing. A simple `AsyncCommand` type is [here](http://stackoverflow.com/a/15743118/263693) and I wrote a more complex one [here](https://nitoasyncex.codeplex.com/SourceControl/latest#Source/Nito.AsyncEx.Mvvm%20(NET45,%20Win8,%20SL4,%20WP75)/AsyncCommand.cs). – Stephen Cleary Jun 18 '13 at 12:16