Using async without await

Viewed 40473

I'd like to make a function async, so I simply add async like this:

public async static void something(){
}

You can see that its return-type is void. I just want this function to be called asynchronously without blocking, since return is void so no await is needed.

But Visual Studio 2012 just cannot compile this, it says that I miss await?

Could you please advise a sample that makes a function async without using await.

2 Answers

Change to

public async Task something(){
  return await Task.Factory.StartNew(() => {
      //TODO:
  }
}

You do not have to wait for the Task returned ;)

so alternatively You can just use a task and remove both async and await and then you get:

public Task something(){
  return Task.Factory.StartNew(() => {
      //TODO:
  }
}

Task is implict void, if you do not use the generic return type argument

Related