-1

my code is compiled properly but getting warning called

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

    private async void Form1_Load(object sender, EventArgs e)
    {
       //await longRunningRoutine();

       await Task.Run(async () =>
       {
           await longRunningRoutine(); ;
       });
       label1.Text = "hello test";

    }

    public async Task longRunningRoutine()
    {
        await Task.Delay(10000);
    }

so tell me what is lack in my code for which i am getting warning? how to fix this problem. thanks

Mou
  • 15,673
  • 43
  • 156
  • 275
  • What does `longRunningRoutine()` actually do? – Leonid Vasilev Feb 20 '17 at 11:09
  • 2
    Replace the sleep with `await Task.Delay(10000);` – Equalsk Feb 20 '17 at 11:09
  • Your async method (longRunningRoutine) lacks await operators and will run synchronously... – Evk Feb 20 '17 at 11:10
  • i update the code and now all fine. thanks – Mou Feb 20 '17 at 11:11
  • Does your real code really have a `Task.Delay()`? If not, then when you try to convert this code to something else (which is not awaitable) you will hit the same issue... But you have already solved it by using `await Task.Run()` anyway. – Matthew Watson Feb 20 '17 at 11:16
  • Possible duplicate of [Warning message in async method saying that it lacks await operators](http://stackoverflow.com/questions/21303133/warning-message-in-async-method-saying-that-it-lacks-await-operators) – Jens Feb 20 '17 at 14:50

3 Answers3

2

If you don't have an awaitable method to await (which you don't), you will have to create a Task to run it, and await that task:

private async void Form1_Load(object sender, EventArgs e)
{
     await Task.Run(() => longRunningRoutine())
}

public void longRunningRoutine()
{
    System.Threading.Thread.Sleep(10000);
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
1

The warning says that you are calling asynchronously longRunningRoutine but this method has not any type of asynchronous request.
To solve this warning you need to call synchronously this method or you need to call asynchronously the Sleep by using Task.Delay

Tinwor
  • 7,765
  • 6
  • 35
  • 56
  • I would imagine that the OP isn't actually interested in calling `Sleep()` - he's just using that to simulate a synchronous operation that takes some time. – Matthew Watson Feb 20 '17 at 11:13
  • I agree with you (for this reason I upvoted your answer) but since he ask why he is getting that error I just wrote an explanation and two possible way to solve it (the third one is already posted by you). – Tinwor Feb 20 '17 at 11:19
1

Your longRunningRoutine method is already asynchronous - so you can remove Task.Run which only waste extra thread

private async void Form1_Load(object sender, EventArgs e)
{
   await longRunningRoutine();
   label1.Text = "hello test";
}

Then to follow conventions suggest to rename method by adding ..Async suffix - it is will save few seconds of your time when you read the code.

Fabio
  • 31,528
  • 4
  • 33
  • 72