Wait for n seconds, then next line of code without freezing form

前端 未结 4 1529
长发绾君心
长发绾君心 2020-12-15 20:45

Hi I am trying to find a method of waiting a number of milliseconds before moving to the next line of code,

I have looked into Thread.Sleep but this will freeze the

4条回答
  •  既然无缘
    2020-12-15 21:14

    Timer should work fine in this case, unless you put Thread.Sleep in its handler or the handler itself takes too much time to complete.

    You haven't specified the UI framework that you use or .Net version, but for the latest .Net you can use async/await. That way, UI would not be frozen while your code awaits for the background task

    void async MyMethod()
    {  
        var result = await Task.Run(() => long_running_code);
    }
    

提交回复
热议问题