Convert void to async return in C #

I read that returning voidfrom a C # call is asyncnot very good. But I have the following scenario:

public async void MainFunction()
{
    await DoSomething()
    await DoSomethingMore()
}

public void DoSomething() 
{
    //some code that I want to execute (fire and forget)
}

public void DoSomethingMore()
{
    //some code that I want to execute (fire and forget)
}

Since I just want this function to execute without any return. Should I store it like this, or should I return Taskfrom DoSomething ()? If I change it to return Task, since my code doesn’t need to return anything at all, what should I return?

+4
source share
1 answer

If I change it to return Task, since my code should not return anything, what should I return to?

, await void ( , , GetAwaiter).

void Task . , async-, . async void , , , threadpool. Async .

, MainFunctionAsync, , .

public async Task MainFunctionAsync()
{
    await DoSomethingAsync();
    await DoSomethingMoreAsync();
}

public Task DoSomethingAsync() 
{
    // Do meaningful async stuff
}

public Task DoSomethingMoreAsync()
{
    // Do more meaningful async stuff
}
+7
source

All Articles