BackgroundWorker.ReportProgress Methode

Definition

Löst das Ereignis ProgressChanged aus.Raises the ProgressChanged event.

Überlädt

ReportProgress(Int32)

Löst das Ereignis ProgressChanged aus.Raises the ProgressChanged event.

ReportProgress(Int32, Object)

Löst das Ereignis ProgressChanged aus.Raises the ProgressChanged event.

ReportProgress(Int32)

Löst das Ereignis ProgressChanged aus.Raises the ProgressChanged event.

public:
 void ReportProgress(int percentProgress);
public void ReportProgress (int percentProgress);
member this.ReportProgress : int -> unit
Public Sub ReportProgress (percentProgress As Integer)

Parameter

percentProgress
Int32

Bereits abgeschlossener Teil des Hintergrundvorgangs in Prozent (1-100).The percentage, from 0 to 100, of the background operation that is complete.

Ausnahmen

Die WorkerReportsProgress-Eigenschaft ist auf false festgelegt.The WorkerReportsProgress property is set to false.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie die ReportProgress-Methode verwendet wird, um den Fortschritt eines asynchronen Vorgangs an den Benutzer zu melden.The following code example demonstrates the use of the ReportProgress method to report the progress of an asynchronous operation to the user. Dieses Codebeispiel ist Teil eines größeren Beispiels, das für die BackgroundWorker-Klasse bereitgestellt wird.This code example is part of a larger example provided for the BackgroundWorker class.

// Abort the operation if the user has cancelled.
// 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 );
   }
}
// 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);
    }
}
' 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 Then
    e.Cancel = True
Else
    If n < 2 Then
        result = 1
    Else
        result = ComputeFibonacci(n - 1, worker, e) + _
                 ComputeFibonacci(n - 2, worker, e)
    End If

    ' Report progress as a percentage of the total task.
    Dim percentComplete As Integer = _
        CSng(n) / CSng(numberToCompute) * 100
    If percentComplete > highestPercentageReached Then
        highestPercentageReached = percentComplete
        worker.ReportProgress(percentComplete)
    End If

End If

Hinweise

Wenn Sie den Hintergrund Vorgang zum Melden des Fortschritts benötigen, können Sie die ReportProgress-Methode aufzurufen, um das ProgressChanged-Ereignis zu erhöhen.If you need the background operation to report on its progress, you can call the ReportProgress method to raise the ProgressChanged event. Der Eigenschafts Wert WorkerReportsProgress muss truesein, oder ReportProgress löst eine InvalidOperationExceptionaus.The WorkerReportsProgress property value must be true, or ReportProgress will throw an InvalidOperationException.

Es liegt an Ihnen, eine sinnvolle Methode zum Messen des Fortschritts des Hintergrund Vorgangs als Prozentsatz der abgeschlossenen Gesamtaufgabe zu implementieren.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.

Der-ReportProgress Methode wird asynchron aufgerufen und wird sofort zurückgegeben.The call to the ReportProgress method is asynchronous and returns immediately. Der ProgressChanged-Ereignishandler wird in dem Thread ausgeführt, der die BackgroundWorkererstellt hat.The ProgressChanged event handler executes on the thread that created the BackgroundWorker.

Siehe auch

ReportProgress(Int32, Object)

Löst das Ereignis ProgressChanged aus.Raises the ProgressChanged event.

public:
 void ReportProgress(int percentProgress, System::Object ^ userState);
public void ReportProgress (int percentProgress, object userState);
member this.ReportProgress : int * obj -> unit
Public Sub ReportProgress (percentProgress As Integer, userState As Object)

Parameter

percentProgress
Int32

Bereits abgeschlossener Teil des Hintergrundvorgangs in Prozent (1-100).The percentage, from 0 to 100, of the background operation that is complete.

userState
Object

Ein eindeutiges Object, das den Benutzerzustand angibt.A unique Object indicating the user state. Wird als UserState-Eigenschaft von ProgressChangedEventArgszurückgegeben.Returned as the UserState property of the ProgressChangedEventArgs.

Ausnahmen

Die WorkerReportsProgress-Eigenschaft ist auf false festgelegt.The WorkerReportsProgress property is set to false.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie die ReportProgress-Methode verwendet wird, um den Fortschritt eines asynchronen Vorgangs an den Benutzer zu melden.The following code example demonstrates the use of the ReportProgress method to report the progress of an asynchronous operation to the user. Dieses Codebeispiel ist Teil eines größeren Beispiels, das für die ToolStripProgressBar-Klasse bereitgestellt wird.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!");
}
Private Sub backgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs)
   ' 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...")
   Dim lastlast As [Decimal] = 0
   Dim last As [Decimal] = 1
   Dim current As [Decimal]
   If requestedCount >= 1 Then
      AppendNumber(0)
   End If
   If requestedCount >= 2 Then
      AppendNumber(1)
   End If
   Dim i As Integer
   
   While i < requestedCount
      ' Calculate the number.
      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
      i += 1
   End While
   
   
   backgroundWorker.ReportProgress(100, "Complete!")
 End Sub

Hinweise

Wenn Sie den Hintergrund Vorgang zum Melden des Fortschritts benötigen, können Sie die ReportProgress-Methode aufzurufen, um das ProgressChanged-Ereignis zu erhöhen.If you need the background operation to report on its progress, you can call the ReportProgress method to raise the ProgressChanged event. Der Eigenschafts Wert WorkerReportsProgress muss truewerden, oder ReportProgress löst eine InvalidOperationExceptionaus.The WorkerReportsProgress property value must true, or ReportProgress will throw an InvalidOperationException.

Es liegt an Ihnen, eine sinnvolle Methode zum Messen des Fortschritts des Hintergrund Vorgangs als Prozentsatz der abgeschlossenen Gesamtaufgabe zu implementieren.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.

Siehe auch

Gilt für: