amibar / SmartThreadPool

A .NET Thread Pool fully implemented in C# with many features
http://www.codeproject.com/Articles/7933/Smart-Thread-Pool
Microsoft Public License
507 stars 182 forks source link

Cancel(true) + WaitForIdle() #34

Closed Olivier-Bastide-Numfem closed 7 months ago

Olivier-Bastide-Numfem commented 1 year ago

Hi Ami,

I'm having an issue and need your advice. Code is very simple :

var pool = new SmartThreadPool(true);
items.ForEach(item => pool.QueueWorkItem(...));
try
{
    pool.Start();
    pool.WaitForIdle();
}
catch (ThreadAbortException)
{
    Thread.ResetAbort();
    pool.Cancel(true);
    pool.WaitForIdle();
}

In each working items, the Thread.Abort is handled to perform cleaning stuff. What I want is, in case of cancellation, to wait for all threads in the pool to have this "cleaning stuff" done. But the pool.WaitForIdle() in the catch doesn't seams to block, as there is some thread still alive doing their cleansing.

Is there a better/correct way to do this with your library ? Thanks Ami !

Vilo176 commented 1 year ago

Ok Ami, after some deeper investigations, I've pointed the problem. Usage of Thread.Abort in my case is not good, because work item are using files, and aborting the thread causes file's handle not being disposed, hence my issues.