How to make another thread sleeping ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • endazyar
    New Member
    • Mar 2010
    • 11

    How to make another thread sleeping ?

    Hi all, do you know how we can make another thread sleeping ?

    As you know, Thread.Sleep(Ti mespan) function makes the current thread sleeping, but i want to make a specific thread sleeping.

    Thanks in advance.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Hmm, I found a way to do this, but I was never quite sure I was doing it right.

    Basically I had a thread that was doing stuff in a loop:
    check for some conditions, perform actions if need be
    release thread so other threads can work
    repeat

    One of the things it checked for was to see if a boolean flag was set, if it was it would perform a special action.
    In your case you could tell your thread to sleep.

    Although in general your threads should spend as much time "sleep"ing as they can.

    Here is an example of what I am talking about. I would stress that this might not be best way, but I have found it to work.
    [code=c#]
    public class Tester
    {
    private const int THREAD_SLEEPTIM E = 100;
    private bool ThreadCanKeepGo ing = true;
    private Thread myT = null;
    private Queue myQ = null;
    private AutoResetEvent autore = new AutoResetEvent( false);//don't know if i need false or true

    public void Init()
    {
    if (myT == null)
    {
    autore.Reset();
    ThreadCanKeepGo ing = true;
    myT = new Thread(new ThreadStart(Bac kThread));
    myT.IsBackgroun d = true;
    myT.Start();
    }
    }
    public void Stop()
    {
    if (myT != null)
    {
    ThreadCanKeepGo ing = false;//set the bool that tells the thread to stop
    autore.Set();//trigger the event so it breaks out of loops(note: if you are "two loops deep" this doesnt work does it?)
    myT.Join();//wait for thread to finish
    myT = null;
    }
    }
    private void BackThread()
    {
    myQ = Queue.Synchroni zed(new Queue());//create the queue on this thread...but the reference is global??

    for (; ThreadCanKeepGo ing; )
    {
    DoStuff();
    #region Process QUEUE
    object qPayload = null;
    lock (myQ.SyncRoot)
    { if (myQ.Count > 0)qPayload = myQ.Dequeue(); }//lock/unlock for the data grab
    if (qPayload!=null ) DealWithMessage (qPayload);//if you got one, send it down

    #endregion

    //sleep or wait for the wakeupsignal, which comes in the Stop() function
    bool evtFired = autore.WaitOne( THREAD_SLEEPTIM E);
    }//end of for loop
    return;
    }
    public void TellBackgroundT hreadToSleep(in t Miliseconds)
    {
    //if you call this from a different thread, it will put your message in the myQ queue
    //Then inside the thread it will be looked at and proceesed in the background thread itself.
    DealWithMessage (Miliseconds);
    }
    private void DealWithMessage (object data)
    {
    if (myT != null)
    {
    if (Thread.Current Thread == myT)
    {//We are running on the background thread so deal with data
    //Do whatever you want with the data
    //for instance:
    int value = (int)data;
    Thread.Sleep(va lue);//the current thread is myT so
    }
    else
    {//this SHOULDNT lock on background thread tasks.
    lock (myQ.SyncRoot) { myQ.Enqueue(dat a); }
    }
    }
    }
    private void DoStuff()
    { }
    } //end of class
    [/code]
    Last edited by Plater; Jun 4 '12, 11:04 PM.

    Comment

    Working...