如何使用WPF后台工作程序

问题描述 投票:172回答:6

我是WPF的初学者,在我的应用程序中,我需要执行一系列初始化步骤,这些步骤需要7-8秒才能完成,在此期间我的UI变得无法响应。为了解决这个问题,我在一个单独的线程中执行初始化:

    public void Initialization()
    {
        Thread initThread = new Thread(new ThreadStart(InitializationThread));
        initThread.Start();
    }

    public void InitializationThread()
    {
        outputMessage("Initializing...");
        //DO INITIALIZATION
        outputMessage("Initialization Complete");
    }

我已经阅读了一些关于BackgroundWorker的文章,以及它应该如何让我保持我的应用程序响应,而不必编写一个线程来执行冗长的任务但我没有成功尝试实现它,任何人都可以告诉我如何使用BackgroundWorker这样做?

谢谢,Eamonn

c# wpf multithreading backgroundworker
6个回答
309
投票
  1. 添加使用
using System.ComponentModel;
  1. 声明Background Worker
private readonly BackgroundWorker worker = new BackgroundWorker();
  1. 订阅活动:
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
  1. 实现两种方法:
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
  // run all background tasks here
}

private void worker_RunWorkerCompleted(object sender, 
                                           RunWorkerCompletedEventArgs e)
{
  //update ui once worker complete his work
}
  1. 根据需要运行worker async。
worker.RunWorkerAsync();
  1. 跟踪进度(可选,但通常很有用) a)订阅ProgressChanged事件并在ReportProgress(Int32)中使用DoWork b)设置worker.WorkerReportsProgress = true;(@zagy的学分)

35
投票

您可能还想研究使用Task而不是后台工作者。

最简单的方法是在你的例子中使用Task.Run(InitializationThread);

使用任务而不是后台工作程序有几个好处。例如,.net 4.5中新的async / await功能使用Task进行线程化。这里有一些关于Task http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx的文档


13
投票
using System;  
using System.ComponentModel;   
using System.Threading;    
namespace BackGroundWorkerExample  
{   
    class Program  
    {  
        private static BackgroundWorker backgroundWorker;  

        static void Main(string[] args)  
        {  
            backgroundWorker = new BackgroundWorker  
            {  
                WorkerReportsProgress = true,  
                WorkerSupportsCancellation = true  
            };  

            backgroundWorker.DoWork += backgroundWorker_DoWork;  
            //For the display of operation progress to UI.    
            backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;  
            //After the completation of operation.    
            backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;  
            backgroundWorker.RunWorkerAsync("Press Enter in the next 5 seconds to Cancel operation:");  

            Console.ReadLine();  

            if (backgroundWorker.IsBusy)  
            { 
                backgroundWorker.CancelAsync();  
                Console.ReadLine();  
            }  
        }  

        static void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)  
        {  
            for (int i = 0; i < 200; i++)  
            {  
                if (backgroundWorker.CancellationPending)  
                {  
                    e.Cancel = true;  
                    return;  
                }  

                backgroundWorker.ReportProgress(i);  
                Thread.Sleep(1000);  
                e.Result = 1000;  
            }  
        }  

        static void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)  
        {  
            Console.WriteLine("Completed" + e.ProgressPercentage + "%");  
        }  

        static void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)  
        {  

            if (e.Cancelled)  
            {  
                Console.WriteLine("Operation Cancelled");  
            }  
            else if (e.Error != null)  
            {  
                Console.WriteLine("Error in Process :" + e.Error);  
            }  
            else  
            {  
                Console.WriteLine("Operation Completed :" + e.Result);  
            }  
        }  
    }  
} 

另外,请参阅以下链接,您将了解qazxsw poi的概念:

Background


5
投票

除了Andrew Orsich所说的,也许你想读一些关于它的例子。

这是使用后台工作程序和进度条的示例代码

http://www.c-sharpcorner.com/UploadFile/1c8574/threads-in-wpf/

这是一个小教程

http://devtoolshed.com/content/c-download-file-progress-bar

并且在本教程结尾处有另一个progressbar + backgroundworker示例的链接。


3
投票

我发现这个(http://www.dotnetperls.com/backgroundworker)包含了@ Andrew的答案中遗漏的其他细节。

我发现非常有用的一件事是工作线程无法访问MainWindow的控件(在它自己的方法中),但是当在主窗口事件处理程序中使用委托时,它是可能的。

WPF Multithreading: Using the BackgroundWorker and Reporting the Progress to the UI. link

1
投票

试试这个链接worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args) { pd.Close(); // Get a result from the asynchronous worker T t = (t)args.Result this.ExampleControl.Text = t.BlaBla; }; ,其中包含如何使用它的示例。

© www.soinside.com 2019 - 2024. All rights reserved.