Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a code like this which executes in a different thread than main thread.
C#
if(_isSTminStarted && (_stMinExpired == false))
            {
                //wait till stMin is expired
                String message = string.Empty;
                do
                {
                    message = "Waiting till STmin expires " + DateTime.Now.ToString("h:mm:ss.fff");
                    _logger.Info(message);
                    

                } while (_stMinExpired == false);               
           
                _isSTminStarted = true;
                _timer.STminTimerStarted(_stMinRecieved);

                CanFrame consecutiveFrame = _fragmentedFrameQueue.Dequeue();
                PublishFrame(consecutiveFrame);
            }


Please see the do while loop , i have added that because i have to wait until a timer expires and _stMinExpired is set to true. Then only i should send next frame.

This flag is set when a System.threading.timer expires. But i don't think this is the ideal way to wait till that time. can you suggest some ways in which i can wait in this thread till that flag is set rather than putting do while loop.
Posted
Updated 12-May-17 7:39am
Comments
walterhevedeich 11-Aug-11 4:35am    
I don't see any problem doing this, unless you don't want to log the message multiple times until _stMinExpired has been set to true.

Try using AutoResetEvent[^].
 
Share this answer
 
Comments
glued-to-code 11-Aug-11 5:14am    
Yes this seems good, i can give it a try.
BobJanova 11-Aug-11 5:39am    
Correct approach, 5.
glued-to-code 11-Aug-11 5:48am    
Hi, Thanks Bob and Firo... This was exactly what i wanted and i have used it and is working as i expected. Thanks again.
As Firo mentions, you want to use one of the event based wait handlers (AutoResetEvent or ManualResetEvent) for this. They are designed for exactly what you ask here: waiting until something happens in another thread at an unknown time.

class Test {
  AutoResetEvent waitHandle = new AutoResetEvent(false);

 void SpawnThread(){
  Thread t = new Thread(ThreadMethod);
  t.Start();
 }
 
 void ThreadMethod(){
  waitHandle.WaitOne();
  CanFrame consecutiveFrame = _fragmentedFrameQueue.Dequeue();
  PublishFrame(consecutiveFrame);
 }

 void SomeLongOtherMethod(){
  DoStuffThatTakesAWhile();
  waitHandle.Set(); // This will release the thread.
 }
}


You should never wait by using a tight loop. That will run a core at 100% CPU and make your app unpopular. Wait handles, and Thread.Sleep as well, are OS level triggers and put your thread into a non-running state that doesn't use CPU time.
 
Share this answer
 
v2
Use Thread.Sleep[^] instead - it can wait for a specific time before your task does anything else.
 
Share this answer
 
Comments
glued-to-code 11-Aug-11 4:37am    
But i don't have specific time until which i should put sleep. When the timer expires , i have to proceed further in the code. So in that case i can't put a sleep for specific time because a case can come where i neednot wait if timer has already expired.
OriginalGriff 11-Aug-11 5:08am    
So instead sleep for 100ms, and check again, or kill your task and re-start it when the timer expires.
BobJanova 11-Aug-11 5:40am    
This would work, but reset events are better if they can be used instead.
glued-to-code 11-Aug-11 5:49am    
Thanks to both of you. i think Bob is right.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900