寻找银弹

致力于探寻软件开发中的本质问题

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

先看下边代码

                while (progressBarTest.Value< 100)
                {
                    System.Threading.Thread.Sleep(100);                    
                   // Dispatcher.Invoke(new Action(() =>                    
                        this.progressBarTest.Value +=1;
                    //}), System.Windows.Threading.DispatcherPriority.Background);
                }

 

就是UI线程进行繁忙操作的时候,会锁死UI,ProgressBar得不到更新。 而等到While循环结束以后才会一下子更新到了100. 如果把注释的两行去掉,那就会如预期一样,ProgressBar能够持续更新。

 再看这个代码

                while (progressBarTest.Value< 100)
                {
                    System.Threading.Thread.Sleep(100);
                    this.progressBarTest.Value += 1;
                    Dispatcher.Invoke(new Action(() =>{}), System.Windows.Threading.DispatcherPriority.Background);
                }

 

 运行的时候会发现, 效果是一样的。 也就是说空的Invoke委托也有相同的效果。 我的想法是, Invoke的时候,UI线程会等待Render线程结束Render才会继续运行。如果UI线程不返回,也就是说当前的DispatcherOperation没有结束不会进行其他的DO的调用,除非显示的请求如此。

还有一个想法 就是 this.progressBarTest.Value += 1; 这行代码的作用实际上是调用了一次BeginInvoke。 期待有时间验证一下。

 

 

posted on 2012-10-31 17:37  hchlee  阅读(180)  评论(0编辑  收藏  举报