C# - Alternative to Thread.Sleep?

前端 未结 4 1989
失恋的感觉
失恋的感觉 2021-01-26 17:13

I\'m doing all this in C#, in Visual Studio 2008.

I want to slow down the work of my algorithm so that the user can watch it\'s work. There is a periodic change visible

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-26 17:48

    I'd guess everything is running out of a single thread. The user probably invokes this algorithm by clicking on a button, or some such. This is handled by your main thread's message queue. Until this event handler returns, your app's GUI cannot update. It needs the message queue to be pumped on regular basis in order to stay responsive.

    Sleeping is almost never a good idea, and definitely not a good idea in the GUI thread. I'm not going to recommend that you continue to use sleep and make your GUI responsive by calling Application.DoEvents.

    Instead, you should run this algorithm in a background thread and when it completes it should signal so to the main thread.

提交回复
热议问题