-1

Possible Duplicate:
Backgroundworker won’t report progress

I am using Background worker in WPF. the problem with is it is not reporting progress. It just update the ProgressBar when it completes task. I want to update it regularly while background task is running. Here is my sample code.

  bworker_doWork(...)
    {
       BindData();//this is database operation which binds data to datagrid.
    }

    bWorker_ProgressChanged(...)
    {
       progressBar.Value=e.ProgressPercentage;//it doesnt update this value
    }

    bWorker_RunWorkerCompleted(..)
    {
       progressBar.Value=100;//max value. control comes here and updates the progress to max value.
    }
Community
  • 1
  • 1
Mujeeb
  • 83
  • 4
  • 14
  • Where do you set ProgressPercentage? Can you provide more code please? – Marcus Oct 11 '12 at 07:01
  • You have to call ReportProgress somewhere in your code. Check out this SO question. http://stackoverflow.com/questions/854548/backgroundworker-wont-report-progress – Yiğit Yener Oct 11 '12 at 07:03

1 Answers1

-1

Are you setting the ProgressPercentage anywhere? (Can´t tell from the code you submitted)

What I think you´ve missed is that you have to call ReportProgress in your code; as such:

myBackgroundWorker.ReportProgress((int)percent, null);

I guess in your code it would look something like this:

bworker_doWork(...)
{
   BindData();//this is database operation which binds data to datagrid.
   bworker.ReportProgress((int)percent);
}

bWorker_ProgressChanged(...)
{
   progressBar.Value=e.ProgressPercentage;//it doesnt update this value
}

bWorker_RunWorkerCompleted(..)
{
   progressBar.Value=100;//max value. control comes here and updates the progress to max value.
}
Marcus
  • 8,230
  • 11
  • 61
  • 88
  • yes. I did miss it, but how would i know the "percent" value. the backgroundWorker should know the status of the operation. if i write bworker.ReportProgress(10). It just update the value of ProgressBar to 10 and to 100 once the task is complete. I want to keep on update its value. Any Idea how to do that – Mujeeb Oct 11 '12 at 08:17
  • It all depends on what you are doing in the _doWork method. If you wish to have a relistic ProgressBar you should try to divide your time-consuming tasks as much as possible. If you have a for-loop in your bworker_doWork method you can always divide the current iteration number with the total number of iterations multiplied by 100 to get the percentage of your progress. For instance, let´s say that you are iterating a collection of objects and doing something with them. Your progress will then be: int progress = currentIteration/objects.Count*100; – Marcus Oct 11 '12 at 08:36