Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access any background worker created in runtime

Scenario: I have a DataGrid in my application in which onclikcing each row, the values get populated in the texboxes below. I'll update the values and when clicking save the process is taking more time to complete.

So i have written a backgroundworker to make the process run asynchronously. When each row is clikced an instance of a backgroundworker is created and the process is accomplished. During that update the user will select the second row of the grid and update that values. So this will create another instance and the process will run in background.

Now when both the update process is running if the user selects the first row of the grid there should be a message showing "The process is still running".

//Code:

' OnClick of the event

         var bw = new BackgroundWorker();
          bw.WorkerReportsProgress = true;
          bw.DoWork += delegate {
              SaveDetails();
          };
          bw.RunWorkerCompleted += delegate {
              MessageBox.Show("Completed");
          };
          bw.RunWorkerAsync();

'Save method

 public void SaveDetails()
    {
        for (int i = 0; i < 10;i++ )
        {
            System.Threading.Thread.Sleep(5000);
            MessageBox.Show("Hi");

        }
    }

How can i access the previously created backgroundworker instance and check the status of the process and display the messgage?

Note: There may be many process running simultaneously, so i should be able to access any process.

Is this possible?

like image 462
A Coder Avatar asked Feb 13 '26 01:02

A Coder


2 Answers

You can store any BackgrounWorker you create in a List to refer to them at any moment, but alternatively you can create a tasks Queue and store there any pending process, so you have only one BackgroundWorker at any time.

Storing the BackgroundWorkers in a List

Create a List(Of BackgroundWorker) which can be accesed anywhere you need in your code, for example. Every time you create a new BackgroundWorker, add it to the List:

mylist.Add(bw)

You a lot of options to access the correct backgroundworker later on. The easiest one is to create your own class which will have an identificator (the row of the DataGrid, for example) and the backgroundworker. This way, your list will be of this class instead of BackgroundWorkers:

myClass.BackgroundWorkerProperty = bw
myClass.id = myId
myList.Add(myClass)

Using a Queue to run the tasks

Create a Queue with a type which has the information to run the task. For example, the row of the DataGrid, if that's enought, that will be type Integer (the index), then add it everytime the backgroundworker is running:

myQueue.Add(myRow)

Everytime the backgroundworker finish, check the Queue and run the next task stored.

like image 94
SysDragon Avatar answered Feb 14 '26 15:02

SysDragon


You can use the Tag property of the DataGridViewRow:

var bw = new BackgroundWorker();
row.Tag = bw;

So you can access it.

like image 23
Alessandro D'Andria Avatar answered Feb 14 '26 13:02

Alessandro D'Andria



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!