关于Thread.Sleep inside backgroundworker的一个问题

A question about Thread.Sleep inside backgroundworker

据我了解,与 GUI 相比,.Net 中的 backgroundworker 运行在不同的威胁下。并且 Thread.Sleep 不推荐,因为它会冻结 GUI。

现在,如果在后台工作人员的 DoWork 中使用 Thread.Sleep ,GUI 会冻结还是这次 Sleep 只会休眠 BW 的线程?还是不推荐?

(我试图通过 Thread.Sleep(1000) 在 DoWork 内暂停 1 秒,似乎它不会干扰 GUI。是否有另一种方法在 BW 内暂停而不是 Thread.Sleep?等待任务需要 BW 不是的同步方法:()

Now if one uses Thread.Sleep inside backgroundworker's DoWork, would GUI freeze or this time Sleep will only sleep the BW's thread?

Thread.Sleep,像所有其他方法一样,在当前线程上运行。所以 Thread.Sleep 导致当前线程休眠。如果它是从 BGW 的 DoWork 调用的,那么它将导致 BGW 的线程休眠。

Is there another way to give pause inside BW rather than Thread.Sleep?

不,但您可以 replace the BackgroundWorker completely with Task.Run,然后您可以使用 await Task.Delay 而不是 Thread.Sleep