Eric Yin
Eric Yin

Reputation: 8973

Use async with void return for fire and forget in C#

I have a method return void in a Web APP

public static void doSomething(){
}

I like it become fire and forget by using async in c# 5.0, how?

Btw, if the void doSomethingQuick() is short-run method, whats the most resource saving method to fire and forget?

If the void doSomethingSlow() is a long-running method, whats the most resource saving method to fire and forget?

Upvotes: 3

Views: 1915

Answers (1)

Cellfish
Cellfish

Reputation: 2192

There are three types of async methods:

async void Foo()
async Task Bar()
async Task<T> Baz()

Note that only the two last can be (a)waited on. Foo in this example is for fire and forget scenarios. You have to remember that async in this case is just an indicator that the method may itself use await. Hence you can declare async void doSomethingQuick() and async void doSomethingSlow(). Just because a method returns a task does not mean it executes on a seperate thread. It may return a completed task.

Upvotes: 1

Related Questions