如何将方法放在Backgroundworker中

时间:2017-03-07 13:03:13

标签: c# progress-bar backgroundworker

我已经制作了一个方法,我想把它放在Backgroundworker上,里面有一个进度条。这是第一个我在后台工作的人。

这是我的代码:

    public void WorkLoad()
    {
        string conStr, sheetName;
        conStr = string.Empty;
        //Get the name of the First Sheet.

        using (OleDbConnection kuneksyon = new OleDbConnection(Excel07ConString))
        {
            using (OleDbCommand utos = new OleDbCommand())
            {
                using (OleDbDataAdapter oda = new OleDbDataAdapter())
                {
                    utos.Connection = kuneksyon;
                    kuneksyon.Open();
                    DataTable dtExcelSchema = kuneksyon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                    kuneksyon.Close();
                    DataTable dt = new DataTable();
                    utos.Connection = kuneksyon;
                    utos.CommandText = "SELECT [Engineer],[SITEID],[SITE NAME],[2G TX Status],[3G TX Status],[WO Status-LTE]  From [" + sheetName + "]";
                    kuneksyon.Open();
                    oda.SelectCommand = utos;
                    oda.Fill(dt);
                    kuneksyon.Close();
                    //Populate DataGridView.
                    ForIssuanceView.DataSource = dt;
                    ForIssuanceView.Columns.Add(" ", " ");
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

假设您将后台工作人员作为班级成员:

player.configure({
  drm: {
    clearKeys: {
      'deadbeefdeadbeefdeadbeefdeadbeef': '18675309186753091867530918675309',
      '02030507011013017019023029031037': '03050701302303204201080425098033'
    }
  }
});

当您打算使用它时,您可以创建并初始化它:

private BackgroundWorker bw;

然后你开始吧:

        bw = new BackgroundWorker();
        bw.WorkerReportsProgress = true;
        bw.ProgressChanged += ProgressChanged;
        bw.DoWork += DoWork;

您应该提供一种方法来进行实际工作:

        bw.RunWorkerAsync();

您可能还提供了一种处理百分比变化的方法。只要在后台工作程序上执行ReportProgress,就会自动调用它。要小心,它会在自己的线程中启动,而不是像你期望的那样在你的UI线程上启动:

    private static void DoWork(object sender, DoWorkEventArgs e)
    {
        // do your actual work and report percentage whenever you find appropriate
        for (var p = 0; p < 100; p++)
        {
            bw.ReportProgress(p);
        }
    }