Async programming

Background worker

https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker.runworkerasync?view=netframework-4.7.2
https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-download-a-file-in-the-background

Fire and forget

Task.Run (() => WriteSchedule(writeFile));

private void WriteSchedule(StreamWriter writeFile)
{
Thread.Sleep(10000);
writeFile.WriteLine("Calculate");
}

Doesn't work, also not when returning a Task:
private async void WriteSchedule(StreamWriter writeFile)
{
Thread.Sleep(10000);
writeFile.WriteLine("Calculate");
}

Calling WriteSchedule will last 10 seconds.

About Tasks

http://stackoverflow.com/questions/10179691/passing-arguments-with-changing-values-to-task-behaviour#10179856

Remark

When using async Task, please note that the ThreadPool by default allocates an amount of threads.
This is usually around 4, booting up more threads (one by one) can take up to a several seconds, dramatically slowing things down.
In my case using 7 threads increased processing time from 5s to 10s.
Solution is preparing the ThreadPool on forehand
The following is blazing fast and nearly completly reduces the creating overhead:

int workerThreads, complete;
ThreadPool.GetMinThreads(out workerThreads, out complete);
ThreadPool.SetMinThreads(100, complete);
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License