Reputation: 279
I know there is a lot of question about this topic, I've reviewed all of them and couldn't find a workable solution.
I'm starting 10 thread simultaneously in WPF appliciation.
First four threads start at the same time, but other jobs start lagging about 5-10 seconds. I share the test data below.
How can I solve this problem?
private void DoParallel()
{
for (int i = 0; i < 10; i++)
{
Task.Factory.StartNew(() =>
{
Console.WriteLine(DateTime.Now.ToString());
DoSomeWork();
});
}
}
}
Upvotes: 0
Views: 127
Reputation: 7354
This is by design. The default TaskScheduler
is using the .NET ThreadPool
, which may be saturated and you have limited control over it.
If you wanted to, you could create your own TaskScheduler
to change this behavior. For example, if desired, you could create one that launches a separate thread.
This behavior would become even more pronounced if your code fired off even more Tasks than it does already:
private void DoParallel()
{
//This will over-saturate the ThreadPool unless you use your own TaskScheduler
for (int i = 0; i < 100; i++)
{
Task.Factory.StartNew(() =>
{
Console.WriteLine(DateTime.Now.ToString());
DoSomeWork();
});
}
}
}
Upvotes: 1