I am trying to debug a multithreaded application where several threads seem to stall, and then continue working minutes later or never again.
I'm sure there is a point of contention between them that is causing this, but I don't know how to find it. Using the
Parallel Stacks
Threads
Task task = new Task((object state) => { DoWork(state).GetAwaiter().GetResult(); }, $"worker_task{i}", TaskCreationOptions.LongRunning);
ConcurrentQueue
GetAwaiter().GetResult();
Httpclient
await
private static void InitWorkers()
{
for(int i = 0; i < max_workers; i++)
{
Task task = new Task((object state) => { DoWork(state).GetAwaiter().GetResult(); }, $"worker_task{i}", TaskCreationOptions.LongRunning);
ProcessingTasks.TryAdd($"worker_task{i}", task);
processedStats.TryAdd($"worker_task{i}", new Stat());
task.Start();
}
}
Your problem is .GetAwaiter().GetResult()
, which blocks the calling thread while waiting for the task to finish.
Don't do that.
If your method is truly async, you don't need to create a Task
at all; you can just call it, and it will do its work asynchronously.
If it performs blocking or CPU-bound calls in its synchronous portion, you should use Task.Run()
to run that in a background thread and wait for the resulting task.