views:

533

answers:

2

I am making an application in C# which uses a winform as the GUI and a separate thread which is running in the background automatically changing things. Ex:

public void run() { while(true) { printMessageOnGui("Hey"); Thread.Sleep(2000); . . } }

How would I make it pause anywhere in the loop, because one iteration of the loop takes around 30 seconds. So I wouldnt want to pause it after its done one loop, I want to pause it on time.

Thanks!

+1  A: 

you can pause a thread by calling thread.Suspend but that is deprecated. I would take a look at autoresetevent for performing you synchronization.

rerun
+2  A: 

How about this:

ManualResetEvent mrse = new ManualResetEvent(false);


public void run() 
{ 
    while(true) 
    { 
        mrse.WaitOne();
        printMessageOnGui("Hey"); 
        Thread.Sleep(2000); . . 
    } 
}

public void Resume()
{
    mrse.Set();
}

public void Pause()
{
    mrse.Reset();
}
Lirik