Wait for a void async method

后端 未结 6 1275
终归单人心
终归单人心 2020-12-02 07:20

How can I wait for a void async method to finish its job?

for example, I have a function like below:

async void LoadBlahBlah()
{
    awa         


        
6条回答
  •  天命终不由人
    2020-12-02 08:12

    You don't really need to do anything manually, await keyword pauses the function execution until blah() returns.

    private async void SomeFunction()
    {
         var x = await LoadBlahBlah(); <- Function is not paused
         //rest of the code get's executed even if LoadBlahBlah() is still executing
    }
    
    private async Task LoadBlahBlah()
    {
         await DoStuff();  <- function is paused
         await DoMoreStuff();
    }
    

    T is type of object blah() returns

    You can't really await a void function so LoadBlahBlah() cannot be void

提交回复
热议问题