Call Method from BackgroundWorker

I hate that my first question seems to have been answered many times, but it's still not easy for me when I remember how to call a method using BackgroundWorker.

I am processing a very large text file using a series of classes and methods. The whole process starts after the user selects a tool element. After that, it will look like this:

  • User selects toolbar item
  • The user selects the file to be processed through the dialog box.
  • Action begins

I think I can wrap everything in BackgroundWorker from the moment the user pulled out the initial dialog box, but now I want to make a way where all the heavy lifting is done in his own copy of BackGroundWorker, I will also add a ProgressBar, but I think I can handle this if I can just start the BackgroundWorker process.

From the top (pseudocode used for example, omitted for brevity):

private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
     string fileName = openSingleFile.FileName;
     processFile(fileName); 
}

static public void processFile(string fileName)
{
// many vars/loops exist but not shown

    foreach (data in bigData)
    {
        processItem(stringA, stringB); // <-- this method is where the expensive work is done 
        x++; 
    }  
} 

I created an instance of BackgroundWorker ...:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // Things go here
}

... and I tried too many things to list, so I went back to the beginning for the presentation above.

If I understand BackgroundWorker, I will need to do the following:

  • Replace processItem (stringA, stringB) in the above code with something like:

    backgroundWorker1.RunWorkerAsync(processItem(stringA, stringB));

... and then make some kind of DoWork call? ... and then make any RunWorkerCompleted call?

, , , . . StackOverflow DOA.

FYI: SO, MSDN DotNetPerls. - , .

+5
3

processItem (stringA, stringB) - ...

, . processFile() . processItem() , , , . , . . , , . , BackgroundWorker, , . , .

- , processFile(). , BackgroundWorker.RunWorkerAsync() , . . DoWork, e.Argument . :

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
        string path = (string)e.Argument;
        processFile(path);
    }

    private void processToolStripMenuItem_Click(object sender, EventArgs e) {
        backgroundWorker1.RunWorkerAsync(openSingleFile.FileName);
        processToolStripMenuItem.Enabled = false;
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
        processToolStripMenuItem.Enabled = true;
    }
+3

- . . .

ToolStripMenuItem_Click , processFile , DoWork.

, , . - . , ReportProgress , .

, , RunWorkerCompleted. , , , , Result , DoWork .

+1
BackgroundWorker bgw;

:

bgw = new BackgroundWorker();
bgw.WorkerReportsProgress = true;
//bgw.WorkerSupportsCancellation = true;

bgw.DoWork += bgw_DoWork;
bgw.ProgressChanged += bgw_ProgressChanged;
bgw.RunWorkerCompleted += bgw_RunWorkerCompleted;

/

    private void ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string fileName = openSingleFile.FileName;

        bgw.RunWorkerAsync(fileName);
    }

    private void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        string fileName = (string)e.Argument;

        processFile(fileName);
    }

    private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        int Progress = e.ProgressPercentage;

        //Update progressbar here
    }

    private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //Job completed
    }
+1

All Articles