-1

I need to first create new task then do some remaining work and then start the task that works with its result.

Simplified example:

static int value;
static async Task work1()
{
    do
    {
        int i;
        for (i = 0; i < 10000000; i++) {} // some calculations
        Console.WriteLine("result1: " + value + " i: " + i);
        await Task.Delay(2000).ConfigureAwait(false);
    } while (condition);
}

static async Task work2()
{
    do
    {
        int i;
        for (i = 0; i < 10000000; i++) {} // some calculations
        Console.WriteLine("result2: " + value + " i: " + i);
        await Task.Delay(2000).ConfigureAwait(false);
    } while (condition);
}

static void Main(string[] args)
{
    Task task;
    int tempvalue = 100;
    if (condition1)
    {
        tempvalue *= 10;
        task = new Task(() => work1());
    } else
    {
        tempvalue -= 5;
        task = new Task(() => work2()); 
    }
    if (tempvalue > 100)
    {
        value = 5;
    } else
    {
        value = tempvalue;
    }
    task.Start();
    // immediately do remaining work
}

this code does exactly what I need but compiler shows following warning:

Warning CS4014  Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

on line:

Task task = new Task(() => work());

should I rather use it like so? Is there any difference?

Task task = new Task(async () => await work());

This is not a duplicate of How to delay 'hot' tasks so they can processed in a set order because after task.Start(); it should do remaining work immediately.

Func<Task> f = () => work();
// do stuff
f(); // blocks thread until work1() or work2() hits await
// do remaining work
LukAss741
  • 771
  • 1
  • 7
  • 24
  • 1
    why do you need to create the task if you're not going to start it? Why not just call `Task.Run` when you need to? – default Nov 19 '18 at 14:56
  • I updated the code so you may get the idea why I need to start it after some other work is done. `Task.Run` would obviously work but in this case it would require placing `if (tempvalue > 100){..}else{..}` code in both if and else in `if (condition1)` – LukAss741 Nov 19 '18 at 15:24
  • I can't add answer to my own question because it was incorrectly marked as duplicate. To get rid of "Warning CS4014 Because this call is not awaited..." error following syntax should be used: `Task task = new Task(() => _ = work());` more on https://learn.microsoft.com/en-us/dotnet/csharp/discards – LukAss741 Nov 25 '18 at 16:23

1 Answers1

-1

The async keyword means that the task within your task is asynchronous and using await will mean that you want to wait for the method work to finish.

You could also use Task.Wait() in order to wait for the method to finish it's execution. But using async await is the better way to do it because it's not blocking the main thread.

  • 1
    The `async` keyword means you can use the `await` keyword, nothing more. – Kenneth K. Nov 19 '18 at 14:44
  • In my actual code there is some more work to do after `task.Start();`. Hence Wait() is not an option. Point of my question is why compiler shows warning for `new Task(() => work());` – LukAss741 Nov 19 '18 at 14:52
  • @LukAss741 because you're not awaiting `work`. `work` is an async Task which you are not awaiting. – default Nov 19 '18 at 14:59
  • @Default I don't await it on purpose in `new Task(() => work());` there is no other work after work(). Main thread should only start work() asynchronously and continue doing remaining code. It works as expected. I am only confused why it shows warning. – LukAss741 Nov 19 '18 at 16:20
  • @LukAss741 well, the compiler doesn't know that you don't want to await it on purpose. But it *does* know that you do not await something that is an `async Task`. If you want to avoid the warning you can write `Task t = work();` and just ignore using t. – default Nov 19 '18 at 17:15
  • `Task t = work();` executes work() right away without waiting for other variables to be calculated. I will be using `Task task = new Task(async () => await work()); ... task.Start();` which works as expected and without warning. Thanks for your advise. – LukAss741 Nov 19 '18 at 18:11