When does the task overlay happen?

After reading the attachments to the TPL from sources such as here , I got the impression that the Task.Wait () call would start, a task that had not yet started (at least using the default scheduler). However, recording a quick demo, for example:

var taskB = new Task( () => { Console.WriteLine("In TaskB"); System.Threading.Thread.Sleep(5000); Console.WriteLine("Leaving TaskB"); }); var taskA = new Task( () => { Console.WriteLine("In TaskA"); System.Threading.Thread.Sleep(500); Console.WriteLine("Waiting on TaskB"); taskB.Wait(); Console.WriteLine("Leaving TaskA"); }); taskA.Start(); taskA.Wait(); 

Causes a dead end. TaskA falls into the string taskB.Wait (), but taskB never starts. I did not bother with the scheduler or anything else, so I'm not quite sure why calling Wait () on taskB will not make it start.

+4
source share
2 answers

Wait() does not call the Start() task. If you call Wait() on an unallocated task, it will wait until it starts and ends until it finishes, the wait time will be stopped, or the wait will be canceled. Since your Wait() call does not contain a cancellation token or timeout, this is infinite to complete the task.

I think that confuses you from the blog:

However, if it is not already running, Wait can be done by the task from the scheduler to which it has been queued, and it is embedded in the current thread to execute it.

The key here is the phrase "do not start execution." This does not mean that Start() not called, but Start() was called, which planned the task and made it ready for execution, but the task did not start.

Start() it is necessary to schedule the task, it does not immediately start execution. This is the main thing in this commercial. If the task is ready to work, but not planned, it can be integrated. But he will not launch a task that was not even planned.

If you look at TaskStatus on MSDN ( See here ), you will see the following values:

  • Created
  • Waiting for activation
  • WaitingToRun
  • Launch
  • WaitingForChildrenToComplete
  • Rantocompletion
  • Canceled
  • Faulted

When you create a task (with a new one or a factory), it is in the Created state. Nothing happens to the task in this state. After its launch, it moves to WaitingForActivation , etc., WaitingForActivation THIS point until it reaches Running , in accordance with this block it can be built-in.

So, a short story, creating a task, simply puts it in the Created state and does not start it if Wait() called. It makes sense?

+15
source

The insert mentioned in this article is different from running a task. Waiting for a task does not start it, which can be demonstrated very easily. Just try the following:

 namespace Test { using System; using System.Threading.Tasks; internal class TestCompile { private static void Main(string[] args) { Task t = new Task(() => Console.WriteLine("Executed!")); t.Wait(5000); Console.WriteLine("After wait..."); Console.ReadKey(); } } } 

You will see that the task never starts ...

Calling task.Wait() does not start the task. This will cause an immediate execution of the task and is "embedded" in the current thread (with the default scheduler) if:

  • The mission has begun, but ...
  • The task is not currently running (since tasks are queued by the scheduler)
  • Task Scheduler (created during build) is the same scheduler as the current one
  • The task has not been canceled.
  • Task not crash

The third item is fuzzy - it can be executed inline on another scheduler, but this requires that another scheduler can execute it immediately, so it depends on the scheduler that supports this.

+2
source

Source: https://habr.com/ru/post/1384992/


All Articles