In my C# I have the following code:
System.Threading.ThreadPool.QueueUserWorkItem(delegate
{
GetWeatherTimetable(new string[] {"London","UK"});
}, null);
Explanation: I create a new ThreadPool.QueueUserWorkItem
to call onto Multi Threading and run the function GetWeatherTimetable
. This function does lots of heavy async work that awaits for user input and things of that nature that cannot be changed and sends ~20 async HTTP Requests using RestSharp
. An example of such would be:
RestClient client = new RestClient();
var getreq = new RestRequest(url, Method.GET);
getreq.AddHeader("accept", "*/*");
var getreqcomplete = await client.ExecuteAsync(getreq);
When running 1 Threaded Task, the whole process finishes anywhere from 5-7 seconds depending on response times from requests. Even with 2 or 3 the difference in time is still marginal and cannot be seen, but when I run a mere 10 tasks, the process takes anywhere from 20-30 seconds and I start seeing inconsistencies in the function and it updates much slower and does everything much slower. I started debugging what was happening and I saw that almost everytime I would call on an await or send a request, the Thread.CurrentThread.ManagedThreadId
would change, sometimes clashing with other threads.
For example: One Thread moves from ThreadID1 to ThreadID2 and another Thread from ThreadID4 changes to ThreadID2 on the same line of code, therefore clashing the onto the same Thread.
Is it possible that this is what is causing the inconsistencies in completion time? I read here about await
having to start a new Thread when it is called and using that instead. Is there anyway that I can disable that and stay on one Thread at a time?