What is the difference between waiting for the async Task function and calling the wait inside the void function?

To understand the question, look at the calls awaitand function definitions of the InitSyncContext()following example.

Based on this, I would like to know how the program will behave in each scenario, because I do not quite understand what is the difference between a call await InitSyncContext(store)and a call awaitinside without returning Task.

For reference, I did research earlier, and I found a similar example here , however, I think it is different in my case.

* The following code is a simplified example from real world code for demo purposes only.

void Main()
{
    Initializer();
}

private async void Initializer()
{
    var store = InitLocalStore();
    await InitSyncContext(store); // <-- Here, await call
    InitFileSync(store);
}

// Here returns Task (without having a return inside. No compile errors)
private async Task InitSyncContext(MobileServiceSQLiteStore store)
{
    await Client.SyncContext.InitializeAsync(store);
}

//------------- Second Method -------------

void Main()
{
    Initializer();
}

private void Initializer()
{
    var store = InitLocalStore();
    InitSyncContext(store); // <-- Here without await call
    InitFileSync(store);
}

// Here is void but with a async call inside
private async void InitSyncContext(MobileServiceSQLiteStore store)
{
    await Client.SyncContext.InitializeAsync(store);
}
+4
3

async Task await void?

! ... . .

"", .. (Task Task<T>), async await.

. async void, async Task - ( ) . ( Main):

One

private async void Initializer()
{
    var store = InitLocalStore();
    await InitSyncContext(store); // <-- Here, await call
    ...
}

// Here returns Task (without having a return inside. No compile errors)
private async Task InitSyncContext(MobileServiceSQLiteStore store)
{
    await Client.SyncContext.InitializeAsync(store);
}

Initializer, , async void. async Task "Async". , InitSyncContext, store . , async await. () , . return. . :

private void Initializer()
{
    var store = InitLocalStore();
    InitSyncContext(store); // <-- Here without await call
    ...
}

// Here is void but with a async call inside
private async void InitSyncContext(MobileServiceSQLiteStore store)
{
    await Client.SyncContext.InitializeAsync(store);
}

! , , , , "", , . InitSyncContext async void. async void , . Task , , . await, , , .

:

private async Task InitializerAsync()
{
    var store = InitLocalStore();
    await InitSyncContextAsync(store);
    ...
}

// Simply return the task that represents the async operation and let the caller await it
private Task InitSyncContextAsync(MobileServiceSQLiteStore store)
{
    return Client.SyncContext.InitializeAsync(store);
}

Main , - async .Result, .Wait().

+3

async Task void?

, , , Async/Await - - .

:

async void - async async void

enter image description here

.

+4

- :

TheadPool.RunTask(()=>InitSyncContext(store)).ContinueWith(()=>InitFileSync(store))

- . . , Client.SyncContext.InitializeAsync(store);

-1

All Articles