BackgroundWorkerHelper

public static class BackgroundWorkerHelper
    {
        public static void Run(DoWorkEventHandler doWork,
                            RunWorkerCompletedEventHandler completed = null,
                            ProgressChangedEventHandler progressChanged = null)
        {
            using (var backgroundWorker = new BackgroundWorker())
            {
                backgroundWorker.DoWork += doWork;
                if (completed != null)
                    backgroundWorker.RunWorkerCompleted += completed;
                if (progressChanged != null)
                {
                    backgroundWorker.WorkerReportsProgress = true;
                    backgroundWorker.ProgressChanged += progressChanged;
                }
                backgroundWorker.RunWorkerAsync();
            }
        }
    }


    public static class BackgroundWorkerHelperOrgainal
    {
        public static BackgroundWorker backgroundWorker { get; set; }
        public static void Run(DoWorkEventHandler doWork,
                            RunWorkerCompletedEventHandler completed = null,
                            ProgressChangedEventHandler progressChanged = null)
        {
            backgroundWorker = new BackgroundWorker();
            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.WorkerSupportsCancellation = true;
            backgroundWorker.DoWork += doWork;
            if (completed != null)
                backgroundWorker.RunWorkerCompleted += completed;
            if (progressChanged != null)
            {
                backgroundWorker.WorkerReportsProgress = true;
                backgroundWorker.ProgressChanged += progressChanged;
            }
            if (!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();

        }
    }
 private void doTime()
        {
            try
            {
                int mm = int.Parse(this.textBox1.Text.Trim());

                BackgroundWorkerHelper.Run((s, e) =>
                {
                    while (1 == 1)
                    {
                        try
                        {
                           
                        }
                        catch (Exception ex)
                        {

                        }
                    }



                },
                (s, e) =>
                {

                });



            }
            catch (Exception ex)
            {
                ;
            }
            finally
            {

            }
        }

 BackgroundWorkerHelper.Run(
                      (s, e) =>
                      {
                          ReturnData rd = new ReturnData();//接收的
                          try
                          {
                              try
                              {
                                  rd = PlugRemote.GetInstance().GetAllCusmail(sd);
                              }
                              catch (Exception ex)
                              {
                                  rd.state = false;
                                  rd.Info = ex.Message;
                              }
                              e.Result = rd;
                          }
                          catch (Exception ex)
                          {
                              rd.state = false;
                              rd.Info = ex.Message;
                              e.Result = rd;
                          }
                      },
                      (s, e) =>
                      {
                          ReturnData rd = (ReturnData)e.Result;
                          if (!rd.state)
                          {
                              this.ShowMessage(rd.Info, 1);
                              return;
                          }
                          pagerControl1.DrawControl(rd.Count);//更新分页控件显示。
                          ReturnData<List<tms_cusmail>> rdt = new ReturnData<List<tms_cusmail>>(rd);
                          this.gridControl1.DataSource = rdt.t;

                      });
BackgroundWorkerHelperOrgainal.Run((s, e) =>
                {
                    BackgroundWorker bg = (BackgroundWorker)s;
                    foreach (var item in listvgs_real)
                    {
                        bg.ReportProgress(0, item);
                        Thread.Sleep(30);
                    }

                },
                (s, e) =>
                {
                    isok = true;
                },
                (s, e) =>
                {
                    if (e.ProgressPercentage == 0)
                    {
                        if (e.UserState != null)
                        {
                            var item = (vgs_real)e.UserState;
                            PointLatLng pl = new PointLatLng(item.OffsetLat, item.OffsetLng);
                            GMapMarkerDirection markerlast = new GMapMarkerDirection(new PointLatLng(item.OffsetLat, item.OffsetLng), Properties.Resources.carsmall, (float)item.Angle, 16, 16);
                            markerlast.IsHitTestVisible = true;
                            markerlast.ToolTipMode = MarkerTooltipMode.Always;
                            markerlast.ToolTipText = string.Format("车速:{0}
地点{1}", item.VDRspeed, item.Address);
                            routesOverlay.Markers.Clear();
                            routesOverlay.Markers.Add(markerlast);

                            gmap.Position = pl;

                        }

                    }
                });
原文地址:https://www.cnblogs.com/wangchuang/p/11041696.html