繁体   English   中英

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

[英]updating datagridview with background worker c#, parsing “RunWorkerCompleted” to each row

我目前是使用后台工作人员的新手,并且已经为我的进程实现了一个简单的更新脚本。 后台工作人员启动我的进程 - python 脚本,启动谷歌的 webdriver 然后“driver.quits()”(该进程被认为已完成)。

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

ON BUTTON CLICK(每行启动python脚本): https://gyazo.com/05e7252a09ef508bc7ebaed753c63469

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

手动 select 所有行(允许我一次在我的 datagridview 中简单地运行每一行,路径值作为一个进程):示例 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# 我应该创建一个后台工作人员还是多个?

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM