Answer the question
In order to leave comments, you need to log in
Is there a way in C# to wait for all tasks that have been added to the execution queue to the thread pool to complete?
There is a method that, under certain conditions, recursively adds itself to the execution queue in the ThreadPool. Is it possible to somehow shade the end of the execution of the last task in this queue?
Answer the question
In order to leave comments, you need to log in
Apparently the documentation is not fashionable to read now ( https://msdn.microsoft.com/en-us/library/system.th...
GetAvailableThreads(Int32, Int32) - Retrieves the difference between the maximum number of thread pool threads returned by the GetMaxThreads method, and the number currently active
.
In the abstract, it's impossible.
And so - what prevents you from doing this in the same method that queues? You do ContinueWith and write processing on "after execution", with processing in the necessary form.
Maybe do something like this?
int working_tasks = 0;
ManualResetEventSlim pre = new ManualResetEventSlim(false);
// здесь запуск тасков
// в каждом таске (в начале) ставим working_tasks = Interlocked.Increment(working_tasks);
// в конце - if(Interlocked.Read(working_tasks) == 0) { pre.Set(); }
pre.Wait();
// здесь то, что должно выполняться после выполнения всех тасков
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question