BackgroundWorker progressBar - Problem implementing ReportProgress when loop occurs in another class
<p>Hi
I am trying to integrate a background worker with a progress bar but cannot get it right.</p>

<p>I am processing some files and all the processing is done in an external class.</p>

<p>My difficulty is that the looping is inside this class,where usually  I do the talking to the backgroundworker.</p>

<p>The good thing is that when processing a file,an event is fired whenever each file has completed processing.</p>

<p>This is my code any suggestions how could this be achieved</p>

<pre><code>    BackgroundWorker _bw = new BackgroundWorker

    private void RunLongProcess()
    {   
        _bw.WorkerReportsProgress = true;
        _bw.WorkerSupportsCancellation = true;        
        _bw.DoWork += DoWork;
        _bw.ProgressChanged += ProgressChanged;
        _bw.RunWorkerCompleted += RunWorkerCompleted;

        _bw.RunWorkerAsync();//start the process

        if (_bw.IsBusy)
           _bw.CancelAsync();
    }

     static void DoWork (object sender, DoWorkEventArgs e)
      {      

        var files=GetFiles();
        int fileCount=files.Count;

        //usually I do a loop here but all the processing is done inside this class so

        var fileProcessor=new FileProcesser();
        fileProcessor.ProcessFiles(files);          

      }
    private void OnFileProcessCompleted(object sender, FileEventArgs e)
    {
        //Event Fired when a file has been processed 
        //How do I update progressBar.Problem cross threading here.

                        //What do I do here?????
        _bw.ReportProgress(e.FileProcessedCount, e);
    }

    ProgressChanged (object sender, ProgressChangedEventArgs e)
    {
          // Update the UI
        labelProgress.Text = e.UserState;
        progressBar.Value = e.ProgressPercentage;
    }


    private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled)
           // Console.WriteLine("You canceled!");
        else if (e.Error != null)
            //Console.WriteLine("Worker exception: " + e.Error.ToString());
        else
           // Console.WriteLine("Complete: " + e.Result);
    }
</code></pre>
