-1

I may get voted as a duplicate of

wrapping-synchronous-code-into-asynchronous-call

However, My question is what if I don't care about the results of the task?

More detail: I want the task to run and whether or not it finishes, I want the task to run again in another thread.

To explain specifically, I have a service that reads a table and gets all the records that have not been processed. The task does it's business, but I don't know if there is one record or 1000 records to be processed in that instance. If it is 1000, it will take several minutes, one will take less than a second, usually the task finds nothing to do. So in that case I don't need to know the results of the previous task nor wait for it to finish.

I have read on async and if I do not consume the await result, I read that it will not continue. Is that true?

The example is:

private async Task MakeRequest()
{
    mainTimer.Stop();
   var task = Task.Run(() => Exec());
   await task;
   mainTimer.Start();
}

does this mean that the task is not ready to run again unless I have the "await task" command?

Frank Cedeno
  • 183
  • 2
  • 7
  • What do you mean run again? If you call `MakeRequest` two times, two tasks will be created. How do you want to call the same task twice? If you call `MakeRequest` without await, second task can start before the first one finishes, if that is what you are asking. – FCin May 30 '18 at 19:12

2 Answers2

1

await task;

States that you want to wait for task completion and continue execute next lines.

When you run this:

Task.Run(() => Exec());

It start to execute new task immediately, doesn't matter did you use await next or not. You can run the same function in a task as many times as you want without using keyword await. But you need make sure that your function can handle concurrent execution.

If you need to get result of your task without using await, you can continue execution on the same thread on which task was started:

task.ContinueWith(task=> {});

Artem Shaban
  • 166
  • 1
  • 10
0

if you don't care about the result, then use a void function:

private async void MakeRequest()
{
   mainTimer.Stop();
   var task = Task.Run(() => Exec());
   await task;
   mainTimer.Start();
}

Also, not consuming a task does not block anything, it will finish even if you don't await it.

Gusman
  • 14,905
  • 2
  • 34
  • 50