Async programming

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 usin 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 completyl 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