The method asyncis executed synchronously until the first await. After that, it will work in the ThreadPool thread (if it does not exist SynchronizationContext).
Thus, the use Task.Factory.StartNewor Task.Runnot recommended, as it tries to parallelize something that has been largely parallel.
, , , Task.Run ( Task.Factory.StartNew) , , .
, " 1" , " 2".
. , , :
protected override void OnStart()
{
var token = _cancellationTokenSource.Token;
_tasks.Add(RunTask1(token));
_tasks.Add(RunTask2(token));
_tasks.Add(Task.Run(() => RunTask3(token)));
}
List<Task> _tasks;
protected override void OnStop()
{
_cancellationTokenSource.Cancel();
Task.WhenAll(_tasks).Wait();
}