Export (0) Print
Expand All

BackgroundWorker.ReportProgress Method (Int32)

Raises the ProgressChanged event.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

public void ReportProgress(
	int percentProgress
)

Parameters

percentProgress
Type: System.Int32

The percentage, from 0 to 100, of the background operation that is complete.

ExceptionCondition
InvalidOperationException

The WorkerReportsProgress property is set to false.

If you need the background operation to report on its progress, you can call the ReportProgress method to raise the ProgressChanged event. The WorkerReportsProgress property value must be true, or ReportProgress will throw an InvalidOperationException.

It is up to you to implement a meaningful way of measuring your background operation's progress as a percentage of the total task completed.

The call to the ReportProgress method is asynchronous and returns immediately. The ProgressChanged event handler executes on the thread that created the BackgroundWorker.

The following code example demonstrates the use of the ReportProgress method to report the progress of an asynchronous operation to the user. This code example is part of a larger example provided for the BackgroundWorker class.

// Abort the operation if the user has canceled. 
// Note that a call to CancelAsync may have set  
// CancellationPending to true just after the 
// last invocation of this method exits, so this  
// code will not have the opportunity to set the  
// DoWorkEventArgs.Cancel flag to true. This means 
// that RunWorkerCompletedEventArgs.Cancelled will 
// not be set to true in your RunWorkerCompleted 
// event handler. This is a race condition. 

if (worker.CancellationPending)
{   
    e.Cancel = true;
}
else
{   
    if (n < 2)
    {   
        result = 1;
    }
    else
    {   
        result = ComputeFibonacci(n - 1, worker, e) + 
                 ComputeFibonacci(n - 2, worker, e);
    }

    // Report progress as a percentage of the total task. 
    int percentComplete = 
        (int)((float)n / (float)numberToCompute * 100);
    if (percentComplete > highestPercentageReached)
    {
        highestPercentageReached = percentComplete;
        worker.ReportProgress(percentComplete);
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft