36

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Eric Yin
  • 8,737
  • 19
  • 77
  • 118
  • Even more interesting is [async Methods correct? Resharper warning](http://stackoverflow.com/questions/15138523/async-methods-correct-resharper-warning) when Resharper issues warning on `async` method having `await` in its body – Gennady Vanin Геннадий Ванин Apr 23 '13 at 01:58
  • @ГеннадийВанинНовосибирск, in that question Resharper is complaining about the two methods that don't have await in them, not the main method which does have await. – Matt Smith Apr 23 '13 at 02:47

2 Answers2

39

I think that maybe you misunderstand what async does. The warning is exactly right: if you mark your method async but don't use await anywhere, then your method won't be asynchronous. If you call it, all the code inside the method will execute synchronously.

Also, you should try to avoid using async void methods, they make handling exceptions difficult.

svick
  • 236,525
  • 50
  • 385
  • 514
  • `async` make the method itself asynchronous or not when the method get called, if no `await` anywhere inside the method? As above sample, if I call `something()`, will it block current thread? – Eric Yin Aug 18 '12 at 08:08
  • Yes, if you have some long-running code inside `something()` (and no `await`), then it will block the thread. That's what the warning is trying to tell you. – svick Aug 18 '12 at 08:45
  • ok, I think I understand wrong. If I do not want `something()` block current thread, I should put `async` in current method and use `something()`(which will not block) right? – Eric Yin Aug 18 '12 at 08:51
  • 10
    `async` is not something that will make your methods magically not block. If you want to run some code on another thread, use `Task.Run()`. – svick Aug 18 '12 at 09:39
  • 8
    Ohh, got it, I misunderstand the concert, async mark the method is going to use async call not mean itself is async. – Eric Yin Aug 18 '12 at 11:03
  • I'm still confused. I want to set an async function running without regard for return values or anything and just continue execution as if nothing happened. You're saying that omitting `await` will turn it into just a regular function call? I don't want to wait for it, I just want it to run and do its thing and die when it's finished. – Patrick Mar 08 '13 at 15:07
  • 2
    @Patrick Then you can use `Task.Run()` to run it on another thread. – svick Mar 08 '13 at 18:38
  • @svick Then why the same method will compile fine in Visual Studio 2019? – Aydin Homay Aug 06 '20 at 21:05
  • @svick, right, await does not mean not blocking. However there is 0 value to async language feature unless at at some point it used to avoid blocking e.g. by not awaiting and async void method. You suggest Task,Run( ) and I agree, and this proves the point that the whole C# async feature is basically useless/incomplete in its current form. It's the software equivalent of the "Emperor's New Clothes". –  Oct 15 '21 at 01:53
-1

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

T. Nielsen
  • 835
  • 5
  • 18