简体   繁体   中英

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

I am currently new to using background worker and have implemented a simple update script for my process. The background worker starts my process -- python script, webdriver that launches google then "driver.quits()" (where the process is considered complete).

The background worker is connected to a button, it pulls a full path directory for my python script from my datagridview column "PD" and uses the path to start a process under my background worker. There are multiple rows with multiple paths to duplicate python scripts in my datagridview. In order to run all of these python files (rows) at once, I use (example 1): (which runs through all of my rows, pulls the python full file path cell value for the row and uses the path to run in a process under background worker). Here I am able to run all of my files (rows) with a click of a button very easily... However, as I am manually selecting each of my rows in my script, background worker runs each specific python file/path (row) though cannot update each row cell value "StatusR" when complete. The background worker updates the "StatusR" column which is a simple textboxcolumn... When each process completes I wish for the background worker to select the row the specific process belongs to and update the label text to "Complete." At the moment, background worker runs through all of my rows, updates the text to each row in the "StatusR" column to "running" from "idle" (the default), and updates one row cell value with "complete" (last selected row by script, coincidental by calling "for each row") when all the drivers finish running. Would appreciate the help:)

ON BUTTON CLICK (python script launches per row): https://gyazo.com/05e7252a09ef508bc7ebaed753c63469

RESULT (after scripts exit): https://gyazo.com/a3ba0b7872074d83462797dabdf9cab2

Manually select all rows (allows me to simply run every row, path value as a process, in my datagridview at once): EXAMPLE 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)

Background Worker:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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