首页 > 代码库 > 使用BackgroundWorker

使用BackgroundWorker

1,WPF应用程序为单线程模型(STAThread),所有UI控件都是主线程创建的,只有主线程能操作UI元素的显示。

2,其他工作线程要维护UI控件的显示,需调用主线程的Dispather,执行Invoke(同步)或BeginInvoke(异步)方法。

3,UI线程如时行耗时的操作,将使UI假死,用户体验较差。应使用后台线程进行耗时操作,并异步更新UI。

4,BackgroundWorker是WPF的一个后台工作类,代码说明一切:

前台:

    <StackPanel>        <ProgressBar Name="progressBar" Height="20" Width="200" Margin="10"></ProgressBar>        <Button Name="btnProcess" Width="100" Click="btnProcess_Click" Margin="5">开始后台任务</Button>        <Button Name="btnCancel" Width="100" Click="btnCancel_Click" Margin="5">取消后台任务</Button>    </StackPanel>

后台:

 public partial class MainWindow : Window    {        BackgroundWorker bgWorker = new BackgroundWorker();        public MainWindow()        {            InitializeComponent();            bgWorker.WorkerReportsProgress = true;            bgWorker.WorkerSupportsCancellation = true;            bgWorker.DoWork += DoWork_Handler;            bgWorker.ProgressChanged += ProgressChanged_Handler;            bgWorker.RunWorkerCompleted += RunWorkerCompleted_Handler;        }        private void btnProcess_Click(object sender, RoutedEventArgs e)        {            if (!bgWorker.IsBusy)            {                bgWorker.RunWorkerAsync();            }        }        private void ProgressChanged_Handler(object sender, ProgressChangedEventArgs args)        {               progressBar.Value = args.ProgressPercentage;                   }        //后台线程运行的程序        private void DoWork_Handler(object sender, DoWorkEventArgs args)        {            BackgroundWorker worker = sender as BackgroundWorker;            for (int i = 1; i <= 100; i++)            {                if (worker.CancellationPending)                {                    args.Cancel = true;                    break;                }                else                {                    worker.ReportProgress(i);                    //因为这在后台线程,要想直接修改UI控件,需用Invoke或BeginInvoke方法                    //this.Dispatcher.Invoke(new Action(() =>                    //{                    //    progressBar.Value = http://www.mamicode.com/i;>//}));                    Thread.Sleep(100);  //因为频繁调用UI线程,为了给UI线程反应时间,这里让后台线程稍顿一下                }            }        }        private void RunWorkerCompleted_Handler(object sender, RunWorkerCompletedEventArgs args)        {            progressBar.Value = 0;            if (args.Cancelled)            {                MessageBox.Show("后台任务已经被取消。", "消息");            }            else            {                MessageBox.Show("后台任务正常结束。", "消息");            }        }        private void btnCancel_Click(object sender, RoutedEventArgs e)        {            bgWorker.CancelAsync();        }    }

 

使用BackgroundWorker