Ejemplo n.º 1
0
 /// <summary>
 /// the main thread watching for state and delta events of scan button
 /// this variant uses the original named events and so will never get the DeltaEvent!
 /// </summary>
 void waitLoop()
 {
     addLog("waitLoop starting...");
     try
     {
         _bWaitLoopRunning = true;
         SystemEvent[] _events = new SystemEvent[3];
         addLog("waitLoop setting up event array...");
         _events[0] = new SystemEvent("StateLeftScan", false, false);
         _events[1] = new SystemEvent("DeltaLeftScan", false, false);
         _events[2] = new SystemEvent("EndWaitLoop52", false, false);
         do
         {
             addLog("waitLoop WaitForMultipleObjects...");
             SystemEvent signaledEvent = SyncBase.WaitForMultipleObjects(
                 -1,                          // wait for ever
                 _events
                 ) as SystemEvent;
             addLog("waitLoop WaitForMultipleObjects released: ");
             if (_continueWait)
             {
                 if (signaledEvent == _events[0])
                 {
                     addLog("######### Caught StateLeftScan ########");
                     onStateScan();
                 }
                 if (signaledEvent == _events[1])
                 {
                     addLog("######### Caught DeltaLeftScan ########");
                     onDeltaScan();
                 }
                 if (signaledEvent == _events[2])
                 {
                     addLog("######### Caught EndWaitLoop52 ########");
                     _continueWait = false;
                 }
             }
             addLog("waitLoop sleep(1)");
             System.Threading.Thread.Sleep(1);
         } while (_continueWait);
         addLog("waitLoop while ended by _continueWait");
     }
     catch (ThreadAbortException ex)
     {
         System.Diagnostics.Debug.WriteLine("waitLoop: ThreadAbortException: " + ex.Message);
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("waitLoop: Exception: " + ex.Message);
     }
     _bWaitLoopRunning = false;
     addLog("...waitLoop EXIT");
 }
 /// <summary>
 /// Waits on messages to be placed on the queue and displays them as they arrive
 /// </summary>
 void ReaderThread()
 {
     using (_reader)
     {
         while (!_shuttingDown)
         {
             //The following call will block this thread until there is either a message
             //on the queue to read or the thread is being signaled to run to prepare
             //for program termination. Since the following call blocks ths thread until
             //it is time to do work it is not subject to the same batter killing
             //affect of other similar looking code patterns ( http://tinyurl.com/6rxoc6 ).
             if (SyncBase.WaitForMultipleObjects(_readerWaitEvent, _reader) == _reader)
             {
                 string msg;
                 _reader.Read(out msg);                          //Get the next message
                 this.Received(this, new ReceivedMessageArgs(msg));
             }
         }
     }
 }
 //######################################################################
 void waitLoop()
 {
     addLog("waitLoop starting...");
     SystemEvent[] _events = new SystemEvent[3];
     addLog("waitLoop setting up event array...");
     _events[0] = new SystemEvent("StateLeftScan1", false, false);
     _events[1] = new SystemEvent("DeltaLeftScan1", false, false);
     _events[2] = new SystemEvent("EndWaitLoop52", false, false);
     try
     {
         do
         {
             //Sleep as long as a snapshot is pending
             while (_bTakingSnapShot && _continueWait)
             {
                 Thread.Sleep(50);
             }
             if (!_continueWait)
             {
                 Thread.CurrentThread.Abort();
             }
             addLog2("waitLoop WaitForMultipleObjects...");
             SystemEvent signaledEvent = SyncBase.WaitForMultipleObjects(
                 -1,                          // wait for ever
                 _events
                 ) as SystemEvent;
             addLog2("waitLoop WaitForMultipleObjects released: ");
             if (_continueWait)
             {
                 if (signaledEvent == _events[0])
                 {
                     addLog2("######### Caught StateLeftScan ########");
                     onStateScan();
                 }
                 if (signaledEvent == _events[1])
                 {
                     addLog2("######### Caught DeltaLeftScan ########");
                     onDeltaScan();
                 }
                 if (signaledEvent == _events[2])
                 {
                     addLog2("######### Caught EndWaitLoop52 ########");
                     _continueWait = false;
                 }
             }
             addLog2("waitLoop sleep(5)");
             System.Threading.Thread.Sleep(5);
         } while (_continueWait);
         addLog("waitLoop while ended by _continueWait");
     }
     catch (ThreadAbortException ex)
     {
         System.Diagnostics.Debug.WriteLine("waitLoop: ThreadAbortException: " + ex.Message);
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine("waitLoop: Exception: " + ex.Message);
     }
     finally
     {
         _events[0].Dispose(); _events[1].Dispose(); _events[2].Dispose();
     }
     addLog("...waitLoop EXIT");
 }