c# Thread issue using Invoke from a background thread

前端 未结 5 712
故里飘歌
故里飘歌 2020-12-11 20:07

I\'ve got thread, which processes some analytic work.

   private static void ThreadProc(object obj)
    {
        var grid = (DataGridView)obj;
        forea         


        
5条回答
  •  旧巷少年郎
    2020-12-11 20:41

    Never, ever, every use Thread.Sleep(0). It doesn't do what you think it does and will cause you nothing but pain. For example, in a tight loop the OS may decide that the thread that just slept is the next one to run. As a result you won't actually yield the thread.

    Try your code again using Thread.Sleep(1) every N iterations where N is about 0.25 to 1.0 seconds worth of work.

    If that doesn't work let me know and we can look at how ThreadProc is created.

    References

    Never Sleep(0) in an Infinite Loop

    EDIT

    Argument for never using Thread.Sleep

    Thread.Sleep is a sign of a poorly designed program.

提交回复
热议问题