Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
后台工作人员-C#_C#_List - Fatal编程技术网

后台工作人员-C#

后台工作人员-C#,c#,list,C#,List,我正在使用BackroundWorker开发一个多线程应用程序。在dou-Work方法中,我调用另一个方法,在该方法中,我使用while语句将大量数据添加到列表中。我的目标是添加列表中显示在GridView中的所有数据。每次数据添加到列表时,gridview都会显示日期,我如何做到这一点?而不是等待while语句完成运行。当while语句向列表中添加值时,该值将插入gridview 它必须在进程中更改,但我不知道如何做。我的方法是创建一个类,该类将保存我来回传递的任何数据。调用ReportPro

我正在使用BackroundWorker开发一个多线程应用程序。在dou-Work方法中,我调用另一个方法,在该方法中,我使用while语句将大量数据添加到列表中。我的目标是添加列表中显示在GridView中的所有数据。每次数据添加到列表时,gridview都会显示日期,我如何做到这一点?而不是等待while语句完成运行。当while语句向列表中添加值时,该值将插入gridview


它必须在进程中更改,但我不知道如何做。

我的方法是创建一个类,该类将保存我来回传递的任何数据。调用
ReportProgress
时,需要百分比增量和对象参数。将类放入此对象参数中,并从
ProgressChanged
事件中,通过
ProgressChangedEventArgs
使此对象可用。然后,您可以读取这些数据,将其输入到要更新的控件中,然后调用该控件上的
Refresh()
方法在UI中更新它,而不冻结界面

编辑:(伪代码,未测试)

private List_customerList=new List();
受保护的无效DoWork(对象发送方,DoWorkEventArgs e)
{
MyDataGridView.DataSource=\u customerList;//这里是设置数据源和绑定的地方
加载客户方法();
}
专用无效荷载\客户\方法()
{
int totalCustomers=20;
int currentCustomer=1;

对于(currentCustomer=1;currentCustomer我的方法是创建一个类,该类将保存我来回传递的任何数据。当您调用
ReportProgress
时,它将需要百分比增量和一个对象参数。您将类放入此对象参数中,并且从
ProgressChanged
事件中,此对象为made可通过
ProgressChangedEventArgs
获得。然后,您可以读取此数据,将其输入到要更新的控件中,然后调用该控件上的
Refresh()
方法在UI中更新它,而无需冻结界面

编辑:(伪代码,未测试)

private List_customerList=new List();
受保护的无效DoWork(对象发送方,DoWorkEventArgs e)
{
MyDataGridView.DataSource=\u customerList;//这里是设置数据源和绑定的地方
加载客户方法();
}
专用无效荷载\客户\方法()
{
int totalCustomers=20;
int currentCustomer=1;

对于(currentCustomer=1;currentCustomer,我建议您阅读backgroundworker。您有所有的信息和示例:


您需要做的就是在ReportProgress中将您的项目作为参数传递,如果ProgressChanged将您的项目添加到网格中。

我建议您阅读backgroundworker上的内容。您在那里有所有信息和示例:


您需要做的只是在ReportProgress中将项目作为参数传递,并在事件ProgressChanged中将项目添加到网格中。

将进度更改事件处理程序添加到您的工作人员

在你的工作方法中

   BackgroundWorker worker = sender as BackgroundWorker;

   worker.ReportProgress(0, new DataObject())
在进程处理程序中

  DataObject data (DataObject)e.UserState;
  yourList.Add(data);

如果您知道自己的进展情况,则可以在ReportProges中发送实际完成的计数,而不是0。

将进度更改事件处理程序添加到您的工作人员

在你的工作方法中

   BackgroundWorker worker = sender as BackgroundWorker;

   worker.ReportProgress(0, new DataObject())
在进程处理程序中

  DataObject data (DataObject)e.UserState;
  yourList.Add(data);

如果您知道自己的进展情况,您可以在ReportProges中发送实际完成的计数,而不是0。

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

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类中

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 );
    }
}
公共类WorkerThread
{
公共无效DoWork(对象发送方,DoWorkEventArgs e)
{
//获取启动此请求的工作进程的句柄
BackgroundWorker workerSender=发件人作为BackgroundWorker;
//循环10次,并向发送对象报告进度
对于(int i=0;i<10;i++)
{
//告诉工人我们要报告正在取得的进展
工作进度报告(一);
睡眠(100);
}
//取消线程并发送回我们取消的线程
workerSender.CancelAsync();
e、 取消=真;
}
public void RunWorkerCompleted(对象发送方,RunWorkerCompletedEventArgs e)
{
Console.WriteLine(“Worker Done!!”);
}
public void ProgressChanged(对象发送方,progresschangedventargs e)
{
//打印出更改的百分比
控制台写入线(如百分比);
}
}

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

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

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类中

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 );
    }
}
公共类WorkerThread
{
公共无效DoWork(对象发送方,DoWorkEventArgs e)
{
//获取启动此请求的工作进程的句柄
BackgroundWorker workerSender=发件人作为BackgroundWorker;
//循环10次,并向发送对象报告进度
对于(int i=0;i<10;i++)
{
//告诉工人我们要报告正在取得的进展
工作进度报告(一);
睡眠(100);
}
//取消线程并发送回我们取消的线程
workerSender.CancelAsync();
e、 取消=真;
}
public void RunWorkerCompleted(对象发送方,RunWorkerCompletedEventArgs e)
{
Console.WriteLine(“Worker Done!!”);
}
public void ProgressChanged(对象发送方,progresschangedventargs e)
{
//打印出更改的百分比
控制台写入线(如百分比);
}
}
我正在使用一个自定义类,您可以使用c