简体   繁体   中英

Wait for async method without blocking the thread

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

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM