Thread to sleep in BackgroundWorker

I wrote a simple application that adds 100,000 "Hello World" lines to the list using BackgroundWorker.

Below is the code of the work that my background worker does in a separate thread:

    private void BgWorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
    {
        int min = 0;

        foreach (var hw in hwList)
        {
            //new ManualResetEvent(false).WaitOne(1);
            Thread.Sleep(1);
            int progress = Convert.ToInt32((Double)min / hwList.Count * 100);
            min++;
            bgWorker.ReportProgress(progress);
        }
    }

    // Updating the progress
    private void BgWorkerOnProgressChanged(object sender, ProgressChangedEventArgs progressChangedEventArgs)
    {
        ProgressBar.Value = progressChangedEventArgs.ProgressPercentage;
    }

Everything works fine, except that when you uninstall Thread.Sleep (1), BackgroundWorker no longer reports progress. (I suppose it needs some time). Pausing a flow for 1 ms actually makes BackgroundWorker a progress report, but it is very slow.

My question is, is there a way to get rid of the sleeping thread, but at the same time do the BackgroundWorker progress report correctly?

, BackgroundWorker , , , .

+4
3

, , , .

 private void BgWorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
    {
        int min = 0;
        int oldProgress = 0;
        foreach (var hw in hwList)
        {
            // new ManualResetEvent(false).WaitOne(1);
            // Thread.Sleep(1);
            int progress = Convert.ToInt32((Double)min / hwList.Count * 100);
            min++;

            // Only report progress when it changes
            if(progress != oldProgress){
                 bgWorker.ReportProgress(progress);
                 oldProgress = progress;
            }
        }
    }

    // Updating the progress
    private void BgWorkerOnProgressChanged(object sender, ProgressChangedEventArgs progressChangedEventArgs)
    {
        ProgressBar.Value = progressChangedEventArgs.ProgressPercentage;
    }
+2

BackgroundWorker SxProgress , :

 int DesiredLinesCount = 100000 ;
 List<string> Lines=new List<string>() ;
 object[] UserObjects = new object[] { Lines } ; 
 SxProgress.Execute("Building lines",DesiredLinesCount,true,false,
                    BuildLines_ExecInThread,UserObjects)) ;

 private bool BuildLines_ExecInThread(int ItemIndex,object[] UserObjects)
{
  // some sleep to slow down the process (demonstration purpose only)
  // if (ItemIndex % 10 ==0) System.Threading.Thread.Sleep(1) ; 
  List<string> Lines= (List<String>)UserObjects[0] ;
  Lines.Add("Hello world") ; 
  return true ; 
}

SxProgress

, , , , .

0

Will this work for WPF? Unfortunately, no : the class includes a form (winforms) with a progress indicator, labels and a stop button.

Possible solution : I have never tried.

This can work by adding 2 links to the WPF project from the Add Link dialog box (on the .NET tab), i.e. "System.Windows.forms" and "WindowsFormsIntegration".

Then in the source code add "using System.Windows.Forms;"
and "using System.Windows.Forms.Integration;"

0
source

All Articles