다음을 통해 공유


BackgroundWorker.ReportProgress 메서드

정의

ProgressChanged 이벤트를 발생시킵니다.

오버로드

Name Description
ReportProgress(Int32)

ProgressChanged 이벤트를 발생시킵니다.

ReportProgress(Int32, Object)

ProgressChanged 이벤트를 발생시킵니다.

ReportProgress(Int32)

Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
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

설명

백그라운드 작업 진행률을 보고 해야 하는 경우 이벤트를 발생 하려면 메서드를 ProgressChanged 호출 ReportProgress 할 수 있습니다. 속성 값은 WorkerReportsProgress 이어야 true합니다. 그렇지 ReportProgress 않으면 .InvalidOperationException

완료된 총 작업의 백분율로 백그라운드 작업의 진행률을 측정하는 의미 있는 방법을 구현하는 것은 사용자의 맡입니다.

메서드에 ReportProgress 대한 호출은 비동기적이며 즉시 반환됩니다. ProgressChanged 이벤트 처리기는 를 만든 BackgroundWorker스레드에서 실행됩니다.

추가 정보

적용 대상

ReportProgress(Int32, Object)

Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
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 입니다. 의 UserState 속성으로 반환됩니다 ProgressChangedEventArgs.

예외

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

설명

백그라운드 작업 진행률을 보고 해야 하는 경우 이벤트를 발생 하려면 메서드를 ProgressChanged 호출 ReportProgress 할 수 있습니다. 속성 값은 WorkerReportsProgress 을 throw해야 하거나 ReportProgress throwInvalidOperationException해야 합니다true.

완료된 총 작업의 백분율로 백그라운드 작업의 진행률을 측정하는 의미 있는 방법을 구현하는 것은 사용자의 맡입니다.

추가 정보

적용 대상