Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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# 如何在Windows窗体应用程序上添加后台工作程序组件?_C#_.net_Winforms_Backgroundworker - Fatal编程技术网

C# 如何在Windows窗体应用程序上添加后台工作程序组件?

C# 如何在Windows窗体应用程序上添加后台工作程序组件?,c#,.net,winforms,backgroundworker,C#,.net,Winforms,Backgroundworker,我想问,如何通过窗口窗体中的代码添加后台工作程序组件?注意,我知道如何从组件托盘中的工具箱添加它,但我想通过代码添加它。欢迎使用任何示例代码 创建BackgroundWorker类的实例,并将处理程序添加到其DoWork事件中。在不同的线程中DoWork、向主线程报告进度和取消异步进程是BackgroundWorker中最重要的功能。下面的示例非常清楚地演示了这三个功能。网上有大量的例子 using System; using System.Threading; using System.Com

我想问,如何通过窗口窗体中的代码添加后台工作程序组件?注意,我知道如何从组件托盘中的工具箱添加它,但我想通过代码添加它。欢迎使用任何示例代码

创建
BackgroundWorker
类的实例,并将处理程序添加到其
DoWork
事件中。

在不同的线程中DoWork、向主线程报告进度和取消异步进程是BackgroundWorker中最重要的功能。下面的示例非常清楚地演示了这三个功能。网上有大量的例子

using System;
using System.Threading;
using System.ComponentModel;

class Program
{
  static BackgroundWorker _bw;

  static void Main()
  {
    _bw = new BackgroundWorker
    {
      WorkerReportsProgress = true,
      WorkerSupportsCancellation = true
    };
    _bw.DoWork += bw_DoWork;
    _bw.ProgressChanged += bw_ProgressChanged;
    _bw.RunWorkerCompleted += bw_RunWorkerCompleted;

    _bw.RunWorkerAsync ("Hello to worker");

    Console.WriteLine ("Press Enter in the next 5 seconds to cancel");
    Console.ReadLine();
    if (_bw.IsBusy) _bw.CancelAsync();
    Console.ReadLine();
  }

  static void bw_DoWork (object sender, DoWorkEventArgs e)
  {
    for (int i = 0; i <= 100; i += 20)
    {
      if (_bw.CancellationPending) { e.Cancel = true; return; }
      _bw.ReportProgress (i);
      Thread.Sleep (1000);      // Just for the demo... don't go sleeping
    }                           // for real in pooled threads!

    e.Result = 123;    // This gets passed to RunWorkerCompleted
  }

  static void bw_RunWorkerCompleted (object sender,
                                     RunWorkerCompletedEventArgs e)
  {
    if (e.Cancelled)
      Console.WriteLine ("You canceled!");
    else if (e.Error != null)
      Console.WriteLine ("Worker exception: " + e.Error.ToString());
    else
      Console.WriteLine ("Complete: " + e.Result);      // from DoWork
  }

  static void bw_ProgressChanged (object sender,
                                  ProgressChangedEventArgs e)
  {
    Console.WriteLine ("Reached " + e.ProgressPercentage + "%");
  }
}
使用系统;
使用系统线程;
使用系统组件模型;
班级计划
{
静态后台工作人员_bw;
静态void Main()
{
_bw=新的后台工作人员
{
WorkerReportsProgress=true,
WorkerSupportsScanCellation=真
};
_bw.DoWork+=bw_DoWork;
_bw.ProgressChanged+=bw_ProgressChanged;
_bw.RunWorkerCompleted+=bw_RunWorkerCompleted;
_bw.RunWorkerAsync(“向工人问好”);
Console.WriteLine(“在接下来的5秒内按Enter键取消”);
Console.ReadLine();
如果(_bw.IsBusy)_bw.CancelAsync();
Console.ReadLine();
}
静态void bw_DoWork(对象发送方,DoWorkEventArgs e)
{

对于(int i=0;i如果您必须在后台线程中执行某些处理,为什么不使用纯线程方法,而不是那种蹩脚的拖放式VB6传统?(我知道这很有争议,而且容易产生不一致的评论)@Davide:对于适合其模型的任务,
BackgroundWorker
比纯线程容易得多。