[英]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";
}
}
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.