Wait for a while without blocking main thread

前端 未结 8 1017
慢半拍i
慢半拍i 2020-12-01 20:50

I wish my method to wait about 500 ms and then check if some flag has changed. How to complete this without blocking the rest of my application?

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 21:19

    Using a timer should do the trick

    if you need to use a thread then here is an example

    void Main()
    {
        System.Threading.Thread check= new System.Threading.Thread(CheckMethod);
        check.Start();
    }
    
    private void CheckMethod()
    {
         //Code
         Thread.Sleep(500);
    }
    

提交回复
热议问题