0

This is our async method which executes for a long time in a Task.

public async Task<int> LongRunningMethod(T p1, T p2, T p3);

Some developers in my team use

Task t = Task.Factory.StartNew(LongRunningMethod(p1,p2,p3));
t.Wait();  //This will suspend main thread and wait for 't' to finish.

While some use

await LongRunningMethod(p1,p2,p3);
// Here too, we simply 'await' till the method finishes.

Note: We have a asynchronous task and we want to wait till that task gets completed.

In short, Which one is the best among the above two constructs ?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • please define "best" – Mong Zhu Dec 08 '17 at 07:16
  • 2
    Why would you not asychronously wait (i.e. *await*) for the task to complete? Also using Task.Factory.StartNew is unlikely to be correct in this case because you run into the nested task problem (e.g. `Task` or `Task>` here). Prefer Task.Run unless you've got a compelling reason not to use it (like existing legacy code that already works with StartNew). – Mike Zboray Dec 08 '17 at 07:16
  • That's a `Task` and not a `Thread`. And no, they're not necessarily the same thing. See [here](https://stackoverflow.com/questions/4130194/what-is-the-difference-between-task-and-thread) for differences, and also [here](https://stackoverflow.com/questions/37419572/if-async-await-doesnt-create-any-additional-threads-then-how-does-it-make-appl). – ProgrammingLlama Dec 08 '17 at 07:19
  • https://stackoverflow.com/questions/9519414/whats-the-difference-between-task-start-wait-and-async-await – Binu Vijayan Dec 08 '17 at 07:40

2 Answers2

2

The point of using Tasks is to execute some Task in parallel from the same context. if you want to start a long running task from a context and just want to wait for the task to finish, both method you showed will work in the same way.

But if you want do any other operation during the execution of the long running task, you can do as

Task taskObj = LongRunningMethod();
/// Do some other operations here...
var result = await taskObj;

But if need the result from the long running task to do some operation you need to await for the task to complete. or you can use Task.ContinueWith()

Binu Vijayan
  • 803
  • 1
  • 9
  • 24
  • 1
    With `await` being introduced I don't see a use case for `ContinueWith`, which was popular in the Pre `await` setup, but it has same limitation of the Task.Wait – Mrinal Kamboj Dec 08 '17 at 07:49
2

In short, Which one is the best among the above two constructs ?

You will know the impact, when system scalability comes in the picture, difference between Task.Wait() and await Task is way they treat the calling / executing thread. When the first one would block till the completion happens, other one using await would free up the caller, but mind it only for a True Asynchronous call, which would mean a IO call, Network call, Database call which is not in memory logical processing, and expose Async APIs, that's when calling thread or the context is freed up. For a true Async call, it would use IO completion ports (queuing mechanism) in windows, to execute the Async pipeline.

This would not happen if at the end of the call chain final method is not true Async.

They look almost similar Wait and await

Indeed, for both continuation will execute post completion, but let's take an example, normally threads getting processed are directly linked to the number of cores in the system, using TPL these thread are invoked from a pool and there are limit to number of thread available. For a high scalable system like 100 K / sec requests, Task.Wait() would time out for a long running method very quickly, as threads are busy idling around and blocked and they will not take new request, but for await no such blockage happens at all.

Practical examples

  • Javascript, NodeJs always go for Async requests by default, no blocking, which is reason why the systems are highly scalable

  • Also consider ConfigureAwait(false), along with await, then it will not look for same thread context to re-enter for the continuation to execute, which is much more efficient

Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74