c# Thread issue using Invoke from a background thread

前端 未结 5 709
故里飘歌
故里飘歌 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条回答
  •  -上瘾入骨i
    2020-12-11 20:41

    The Invoke statement will wait until the main thread's message pump isn't busy, and can handle a new message. If your main thread is busy, the Invoke will hang.

    In your case, it looks like your top code is running in a tight loop, so there's never a chance for the Invoke in the bottom code to actually run. If you change the Thread.Sleep in your upper code block to something with a time in it, hopefully that will give your main thread a chance to handle the .Invoke call.

    Depending on what your main application thread is doing, you may need to actually finish your first loop before any of the .Invoke calls will run - if that's the case, I can post some modified code that might work better.

提交回复
热议问题