How to pause/suspend a thread then continue it?

后端 未结 3 1325
北恋
北恋 2020-12-03 21:11

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:

         


        
3条回答
  •  温柔的废话
    2020-12-03 21:58

    var 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();
    

提交回复
热议问题