One of the things that I find difficult to understand in multi-threaded programming is the fact that when one thread reaches the line that calls WaitOne() , how do I know which other threads are involved? Where and how can I find (or understand) how WaitHandle receives a signal? For example, I'm looking at this code right now:
private void RunSync(object state, ElapsedEventArgs elapsedEventArgs) { _mutex.WaitOne(); using (var sync = GWSSync.BuildSynchronizer(_log)) { try { sync.Syncronize(); } catch(Exception ex) { _log.Write(string.Format("Error during synchronization : {0}", ex)); } } _mutex.ReleaseMutex(); _syncTimer.Interval = TimeBeforeNextSync().TotalMilliseconds; _syncTimer.Start(); }
There are several such methods in the file (for example, RunThis (), RunThat ()). These methods run inside the Windows service and are called when the timer expires. Each of these methods is called using different timers and is configured as follows:
//Synchro var timeBeforeFirstSync = TimeBeforeNextSync(); _syncTimer = new System.Timers.Timer(timeBeforeFirstSync.TotalMilliseconds); _syncTimer.AutoReset = false; _syncTimer.Elapsed += RunSync; _syncTimer.Start();
I understand that when the timer expires, the RunSync method will be launched. But when it hits the WaitOne() , the thread is blocked. But who is waiting for this? What "other" stream will send the signal?
source share