Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass variable to RunWorkerCompleted() after DoWork() is done

I am using VS 2015 winform background workers to do some tasks.

I have DoWork() method that uses List<string> myList, and it removes each entry in the list once certain process is complete. My goal is to let users know which entry in myList was not processed (since it deletes an entry after every process, whatever is left in the end is the one that is NOT processed). To do so, I was going to pass the myList variable to RunWorkerCompleted(), but not only do I not know how to do this, I am not even sure if this is the best way of doing it.

Are there better ways to let users know which entry in myList was not processed, and if not, how would I pass a variable from DoWork() to RunWorkerCompleted()?

Current RunWorkerCompleted() method:

private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if(e.Error != null)
    {
        MessageBox.Show(e.Error.ToString());
    }
    else
    {
        MessageBox.Show("Done!");
    }
}

Basically, I want the MessageBox to display which entries are not processed when it's done running a task

like image 242
djskj189 Avatar asked Dec 02 '16 14:12

djskj189


Video Answer


1 Answers

You can use the result of the RunWorkerCompletedEventArgs

See this

OK for some people who just don't want to read, use this

private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
    //Do your work
    e.Result = mylist;
}

private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if(e.Error != null)
    {
        MessageBox.Show(e.Error.ToString());
    }
    else
    {
        ShowResult(e.Result as List<string>);
    }
}
like image 98
Emad Avatar answered Oct 19 '22 11:10

Emad