I'm developing an application using WPF
and C#
.
I have the following code:
var tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;
Task task = Task.Factory.StartNew(() =>
{
// Some action that returns a boolean - **CODE_A**
}).ContinueWith((task2) =>
{
result= task2.Result;
if (!result)
{
//Another action **CODE_B**
}
});
}, token);
Usually CODE_A starts running immediately, and after less than a second, CODE_B starts executing.
But, sometimes it takes for the task created with Task.Factory.StartNew
more than 5 seconds to begin (once it begins, the execution is quick as usual).
I don't understand why does it take so long for the task to start running? Can I somehow influence the task priority, so it would start running immediately in all scenarios? I guess (it is only an assumption) the task is scheduled by the Task scheduler to execute later? Is there a way for me to force the task to run immediately all the time?