WPF进度控制调度仍要堵塞UI线程 [英] WPF ProgressBar Control Dispatch still blocking UI Thread

查看:416
本文介绍了WPF进度控制调度仍要堵塞UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF试图在更新进度条的后台运行一个线程。我不婉让我运行下面的code阻塞UI线程。然而,用户界面​​仍然受阻。它似乎很简单,我究竟做错了什么?

  Dispatcher.BeginInvoke(
        (的ThreadStart)委托(){
            对于(双I = progressBar_ChangeProgress.Minimum;
                I< progressBar_ChangeProgress.Maximum;
                我++)
                {
                    为(中间体B = 0; b将亿; b ++){}
                    progressBar_ChangeProgress.Value =我;
                }
            EnableAllInputControls();
        },DispatcherPriority.Background);
 

解决方案

为什么不利用的BackgroundWorker 在这种情况下...

 无效围棋()
        {
            BackgroundWorker的工作人员=新的BackgroundWorker();
            worker.WorkerReportsProgress = TRUE;
            worker.ProgressChanged + =新ProgressChangedEventHandler(worker_ProgressChanged);
            worker.DoWork + =新DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerAsync();
        }

        无效worker_ProgressChanged(对象发件人,ProgressChangedEventArgs E)
        {
            progressBar_ChangeProgress.Value = e.ProgressPercentage;
        }

        无效worker_DoWork(对象发件人,DoWorkEventArgs E)
        {
            BackgroundWorker的工人=发件人为BackgroundWorker的;
            为(中间体B = 0; b将100; b + +)
            {
                Thread.sleep代码(100);
                worker.ReportProgress(B);
            }
        }
 

更新:

如果你想使用调度;优先级设置为正常并在后台线程然后调用UI线程的方法来提供更新进行处理。

 无效围棋()
        {
            的ThreadStart开始=委托()
            {
                //这是发生在后台线程
                的for(int i = 0; I< 100;我++)
                {
                    //这是速度变慢;没有真正的意义
                    Thread.sleep代码(100);

                    //这将封送我们回到UI线程
                    Dispatcher.Invoke(DispatcherPriority.Normal,
                                         新的动作< INT>(更新),我
                                         );
                }

            };

            新的Thread(开始)。开始();
        }

        无效更新(int值)
        {
            //这是发生在UI线程
            _progressBar.Value =价值;
        }
 

I am using WPF trying to run a Thread in the background that updates a progress bar. I don't wan to block the UI thread so I am running the following code. However the UI still blocked. It seems so simple, what am I doing wrong?

       Dispatcher.BeginInvoke(
        (ThreadStart) delegate(){                   
            for(double i = progressBar_ChangeProgress.Minimum;
                i < progressBar_ChangeProgress.Maximum;
                i++)
                {
                    for (int b = 0; b < 100000000; b++) { }
                    progressBar_ChangeProgress.Value = i;
                }
            EnableAllInputControls();
        }, DispatcherPriority.Background);

解决方案

Why not leverage the BackgroundWorker in this scenario...

        void Go()
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerAsync();
        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar_ChangeProgress.Value = e.ProgressPercentage;
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            for (int b = 0; b < 100; b++) 
            {
                Thread.Sleep(100);
                worker.ReportProgress(b);
            }
        }

UPDATE:

If you are wanting to use the Dispatcher; set the priority to Normal and perform the processing on the background thread then calling a method on the UI thread to provide the update.

        void Go()
        {
            ThreadStart start = delegate()
            {
                //this is taking place on the background thread
                for (int i = 0; i < 100; i++)
                {
                    //this is slowing things down; no real relevance
                    Thread.Sleep(100);

                    //this will marshal us back to the UI thread
                    Dispatcher.Invoke(DispatcherPriority.Normal,
                                         new Action<int>(Update), i
                                         );
                }

            };

            new Thread(start).Start();
        }

        void Update(int value)
        {
            //this is taking place on the UI thread
            _progressBar.Value = value;
        }

这篇关于WPF进度控制调度仍要堵塞UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆