BackgroundWorker-C# [英] BackgroundWorker - C#

查看:105
本文介绍了BackgroundWorker-C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用BackroundWorker开发多线程应用程序.在Do_Work方法中,我调用另一个方法,在该方法中,我使用while语句将大量数据添加到列表中.我的目标是添加列表中要显示在GridView中的所有数据.我该怎么做,以便每次将数据添加到列表时,gridview都会更新?而不是等到while语句完成运行.当while语句将一个值添加到列表时,该值会插入到gridview中吗?

I am developing a multithreading application using BackroundWorker. In the Do_Work method I call another method, in that method I add a lot of data into a list using a while-statement. My goal is to add all the data that in the list to show in a GridView. How can I do that so every time data adds to the list, the gridview uppdates? Instead of waiting until that the while-statement has finished running. When the while-statment adds a value to the list, the value inserts into the gridview?

它必须在ProgressChanged中,但是我不知道该怎么做.

It must be in the ProgressChanged, but I dont know how to do that.

推荐答案

这可能会有所帮助.我有一个名为WorkerThread的类,它可以完成我们想要的工作

This might help. I have a class called WorkerThread that does the work we want

static void Main( string[] args )
{
   // create the background worker object
   BackgroundWorker _worker = new BackgroundWorker();

   // tell the background worker it can be cancelled and report progress
   _worker.WorkerReportsProgress = true;
   _worker.WorkerSupportsCancellation = true;

   // a worker thread object where the actual work happens
   WorkerThread thread = new WorkerThread();

   // add our event handlers
   _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( thread.RunWorkerCompleted );
   _worker.ProgressChanged += new ProgressChangedEventHandler( thread.ProgressChanged );
   _worker.DoWork += new DoWorkEventHandler( thread.DoWork );

   // start the worker thread
   _worker.RunWorkerAsync();

   // wait for it to be completed
   while( !_worker.CancellationPending )
   {
      // sleep for a second
      Thread.Sleep( 1000 );
    }

    Console.ReadKey();

}

现在在WorkerThread类中

Now in the WorkerThread class

public class WorkerThread
{
    public void DoWork( object sender, DoWorkEventArgs e )
    {
       //get a handle on the worker that started this request
       BackgroundWorker workerSender = sender as BackgroundWorker;

       // loop through 10 times and report the progress back to the sending object
       for( int i = 0; i < 10; i++ )
       {
          // tell the worker that we want to report progress being made
          workerSender.ReportProgress( i );
          Thread.Sleep( 100 );
       }

       // cancel the thread and send back that we cancelled
       workerSender.CancelAsync();
       e.Cancel = true;

    }

    public void RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e )
    {
       Console.WriteLine( "Worker Done!!" );          
    }

    public void ProgressChanged( object sender, ProgressChangedEventArgs e )
    {
      // print out the percent changed
      Console.WriteLine( e.ProgressPercentage );
    }
}

我正在使用自定义类,您可以在您的类中使用一种创建后台工作程序的方法.只需修改ProgressChanged事件中的代码即可.

I am using a custom class, you could use a method in your class that is creating the background worker. Just modify the code in the ProgressChanged event.

这篇关于BackgroundWorker-C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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