使用后台工作程序 c# 更新 datagridview,将 "RunWorkerCompleted" 解析为每一行

updating datagridview with background worker c#, parsing "RunWorkerCompleted" to each row

我目前不熟悉使用后台工作程序,并为我的流程实施了一个简单的更新脚本。后台工作人员启动我的进程——python 脚本,启动 google 然后是“driver.quits()”(进程被认为完成)的网络驱动程序。

后台工作者连接到一个按钮,它从我的 datagridview 列“PD”中提取我的 python 脚本的完整路径目录,并使用该路径在我的后台工作者下启动一个进程。在我的 datagridview 中,有多行有多个路径可以复制 python 脚本。为了一次 运行 所有这些 python 文件(行),我使用(示例 1):(运行 遍历我的所有行,拉出 python 行的完整文件路径单元格值,并在后台工作程序下的进程中使用 运行 的路径)。在这里,我可以通过单击按钮非常轻松地 运行 我的所有文件(行)...但是,当我手动 select 我的脚本中的每一行时,后台工作人员运行s 每个特定的 python file/path(行)虽然在完成时无法更新每个行单元格值“StatusR”。后台工作人员更新“StatusR”列,这是一个简单的文本框列... 当每个进程完成时,我希望后台工作人员到 select 特定进程所属的行并更新标签文本到“完成。” 目前,后台工作人员 运行s 遍历我的所有行,将“StatusR”列中每一行的文本更新为“运行ning” “idle”(默认值),并在所有驱动程序完成 运行ning 时将一行单元格值更新为“complete”(脚本最后 selected 行,通过调用“for each row”巧合) .非常感谢您的帮助:)

单击按钮(python 每行启动脚本):https://gyazo.com/05e7252a09ef508bc7ebaed753c63469

结果(脚本退出后):https://gyazo.com/a3ba0b7872074d83462797dabdf9cab2

手动select所有行(允许我简单地运行每一行,路径值作为一个过程,在我的数据网格视图中一次):示例1

foreach (DataGridViewRow row in dataGridView1.Rows)
      {
         if (row.Cells[3].Value.ToString().Equals("3")) #this value is set to 3 for all of my columns  to easily select them all --constant :)
          {
                 dataGridView1.ClearSelection();
                 row.Selected = true;

                 #script to read python path and execute background worker with respective path (datagridview column value)

后台工作者:

   ...............
  
        var worker = new BackgroundWorker();
        worker.WorkerReportsProgress = false;
        worker.WorkerSupportsCancellation = false;
        dataGridView1.SelectedRows[0].Cells[8].Value = "Running";
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        worker.RunWorkerAsync();
    }
    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        var p = new System.Diagnostics.Process();
        p.StartInfo.FileName = "C://Users//Win_10//AppData//Local//Programs//Python//Python38-32//python.exe";
        p.StartInfo.Arguments = "C://Users//Win_10//AppData//Local//Programs//Python//Python38-32//harrypotterbackground.py";
        p.Start();
        p.WaitForExit();
    }

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        dataGridView1.ClearSelection();
        int inde = dataGridView1.CurrentRow.Index;
        dataGridView1.Rows[inde].Selected = true;
        dataGridView1.CurrentRow.Cells[8].Value = "idle";
    }
}

C# should I create one Background worker or many?

public void SomeEventHandlerMaybe(object sender, EventArgs e) {
  // do something

  var bw = new BackgroundWorker();
  bw.ReportsProgress = true;
  bw.DoWork += delegate {
    // do work. You can use locals from here MY FIXMY FIXMY FIX
  };
  bw.ProgressChanged += delegate { ... };
  bw.RunWorkerCompleted += delegate {
    // do something with the results.
  };
  bw.RunWorkerAsync();
}