共用方式為


BackgroundWorker.ReportProgress 方法

定義

引發 ProgressChanged 事件。

多載

名稱 Description
ReportProgress(Int32)

引發 ProgressChanged 事件。

ReportProgress(Int32, Object)

引發 ProgressChanged 事件。

ReportProgress(Int32)

來源:
BackgroundWorker.cs
來源:
BackgroundWorker.cs
來源:
BackgroundWorker.cs
來源:
BackgroundWorker.cs
來源:
BackgroundWorker.cs

引發 ProgressChanged 事件。

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

參數

percentProgress
Int32

背景操作完成的百分比,從0到100。

例外狀況

WorkerReportsProgress 屬性會設定為 false

範例

以下程式碼範例示範 ReportProgress 使用此方法向使用者報告非同步操作的進度。 此程式碼範例是本類別更大範例 BackgroundWorker 的一部分。

// 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
{
    result = n < 2
        ? 1
        : ComputeFibonacci(n - 1, worker, e) +
                 ComputeFibonacci(n - 2, worker, e);

    // Report progress as a percentage of the total task.
    int percentComplete =
        (int)(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

備註

如果你需要背景操作來回報其進度,你可以呼叫 ReportProgress 該方法來提出 ProgressChanged 事件。 WorkerReportsProgress財產價值必須是 true,否則ReportProgress會拋出 InvalidOperationException

你必須實施一個有意義的方法,以百分比衡量背景作業的進度,佔總完成任務的百分比。

呼叫該 ReportProgress 方法為非同步,並立即回傳。 ProgressChanged事件處理器會在建立 BackgroundWorker.

另請參閱

適用於

ReportProgress(Int32, Object)

來源:
BackgroundWorker.cs
來源:
BackgroundWorker.cs
來源:
BackgroundWorker.cs
來源:
BackgroundWorker.cs
來源:
BackgroundWorker.cs

引發 ProgressChanged 事件。

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

參數

percentProgress
Int32

背景操作完成的百分比,從0到100。

userState
Object

唯一 Object 代表使用者狀態。 作為 UserStateProgressChangedEventArgs.

例外狀況

WorkerReportsProgress 屬性會設定為 false

範例

以下程式碼範例示範 ReportProgress 使用此方法向使用者報告非同步操作的進度。 此程式碼範例是本類別更大範例 ToolStripProgressBar 的一部分。

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

備註

如果你需要背景操作來回報其進度,你可以呼叫 ReportProgress 該方法來提出 ProgressChanged 事件。 WorkerReportsProgress財產價值必須 true,或ReportProgress會拋出 。InvalidOperationException

你必須實施一個有意義的方法,以百分比衡量背景作業的進度,佔總完成任務的百分比。

另請參閱

適用於