Good thing I'm doing now (using your ideas) and it seems to work:
I announced a ManualResetEvent list:
Private m_waitHandles As List(Of Threading.ManualResetEvent)
The process accepts incoming Tcp connections and starts one thread for each connection. Therefore, in the new client handler, I added this code:
Dim waitHandle As Threading.ManualResetEvent waitHandle = New Threading.ManualResetEvent(True) SyncLock (DirectCast(m_waitHandles, IList).SyncRoot) m_waitHandles.Add(waitHandle) End SyncLock ''
The last thing to do is change the Stop method to make sure that the Stop operation will not be executed on any thread inside NonStoppableMethod:
SyncLock (DirectCast(m_waitHandles, IList).SyncRoot) If m_waitHandles.Count > 0 Then Threading.WaitHandle.WaitAll(m_waitHandles.ToArray()) End If End SyncLock
Iām not sure that this is done correctly, because for the first time I do such things. Do you feel that this is normal and is a good approach?
Thanks to everyone, comrades!
source share