Export (0) Print
Expand All

BackgroundWorker.ReportProgress Method (Int32, Object)

Raises the ProgressChanged event.

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

public void ReportProgress (
	int percentProgress,
	Object userState
)
public void ReportProgress (
	int percentProgress, 
	Object userState
)
public function ReportProgress (
	percentProgress : int, 
	userState : Object
)
Not applicable.

Parameters

percentProgress

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

userState

The state object passed to RunWorkerAsync.

Exception typeCondition

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 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 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 ToolStripProgressBar class.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // This method will run on a thread other than the UI thread.
    // Be sure not to manipulate any Windows Forms controls created
    // on the UI thread from this method.
    backgroundWorker.ReportProgress(0, "Working...");
    Decimal lastlast = 0;
    Decimal last = 1;
    Decimal current;
    if (requestedCount >= 1)
    { AppendNumber(0); }
    if (requestedCount >= 2)
    { AppendNumber(1); }
    for (int i = 2; i < requestedCount; ++i)
    {
        // Calculate the number.
        checked { current = lastlast + last; }
        // Introduce some delay to simulate a more complicated calculation.
        System.Threading.Thread.Sleep(100);
        AppendNumber(current);
        backgroundWorker.ReportProgress((100 * i) / requestedCount, "Working...");
        // Get ready for the next iteration.
        lastlast = last;
        last = current;
    }


    backgroundWorker.ReportProgress(100, "Complete!");
}

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft