2013-08-26 2 views
5

백그라운드 작업자를 중지시키고 작업중인 모든 프로세스를 종료 할 수있는 단추를 만들고 싶습니다. 내가 루프를 재설정하고 내가 BackgroundWorker에 중지 0 %로 진행률 표시 줄을 반환 할VB.net 백그라운드 작업자 중지

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

      Try 
       If BackgroundWorker1.IsBusy <> True Then 
        BackgroundWorker1.RunWorkerAsync() 
       End If 
      Catch ex As Exception 
      End Try 

     End Sub 

     Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 

      Dim counter As Integer = 1 

      Do 

      'updated code with stop function---------------- 
      BackgroundWorker1.WorkerSupportsCancellation = True 
      If BackgroundWorker1.CancellationPending Then 
       e.Cancel = True 
       ProgressBar1.Value = 0 
       Exit Do 
      End If 
      'updated code with stop function---------------- 

      ListBox1.Items.Add(counter) 

      ProgressBar1.Value = ((counter - 1)/limit) * 100 
      counter = counter + 1 
      Loop While(counter <= 999999999999999999) 

     End Sub 

     Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged 
      Try 
      Catch ex As Exception 
      End Try 
     End Sub 

     Private Sub BackgroundWorker1_Completed(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted 
      Try 
      Catch ex As Exception 
      End Try 
     End Sub 

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
      System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False  
     End Sub 

     'updated code with stop function---------------- 
     Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click 
       If BackgroundWorker1.IsBusy Then 

        If BackgroundWorker1.WorkerSupportsCancellation Then     
        BackgroundWorker1.CancelAsync() 
        End If 
       End If 
     End Sub 
     'updated code with stop function---------------- 

:

여기 내 샘플 BackgroundWorker 구성 코드입니다.

이것이 가능합니까?


위 코드가 업데이트되었으며 현재 정상적으로 작동 중입니다.

나는 내 할 일 루프 내부에이 코드를 추가 한 :

 BackgroundWorker1.WorkerSupportsCancellation = True 
     If BackgroundWorker1.CancellationPending Then 
      e.Cancel = True 
      ProgressBar1.Value = 0 
      Exit Do 
     End If 

내가 노동자 정지 버튼을 생성 :

BackgroundWorker 구성 클래스는 당신이 호출 할 필요가 방법 CancelAsync()을 가지고
Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click 
      If BackgroundWorker1.IsBusy Then 

       If BackgroundWorker1.WorkerSupportsCancellation Then     
       BackgroundWorker1.CancelAsync() 
       End If 
      End If 
    End Sub 

답변

9

bgw의 실행을 취소하십시오.

당신은 true로 Backgroundworker.WorkerSupportsCancellation 속성을 설정해야하고, while 루프 안에 당신은 값이 CancelAsync() 메서드 호출을 나타냅니다 true입니다 어떠했는지 CancellationPending 속성을 확인해야합니다.

CancellationPending이 true로 평가되면, 당신이 (당신이 이미 했어야 ) 원하는 값에 ProgressBar 구성 값을 설정하는 오버로드 ReportProgress() (Docu) 방법 중 하나를 부를 것이다.

편집 : 당신이 RunworkerCompleted 이벤트 내부의 RunWorkerCompletedEventArgsCancelled 속성을 확인할 수 있도록 true로 DoWorkEventArgsCancel 속성을 설정해야합니다.

또한 UI 스레드에있는 컨트롤에 액세스하면 안됩니다. ProgressChanged (Docu) 이벤트를 사용하는 것이 좋습니다.

참조 : 내가 작업 정지 버튼과 내 원래의 코드를 업데이트 한 작정 BackgroundWorker Docu

+0

감사합니다. 나는 그 (것)들 사이에서 "정지 기능을 가진 개정하는 부호"를 말한 코멘트를 두었다 –

-1
Public Class Form1 
    Private iVal As Integer = 0 
    Private Sub bgw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork 
     For iVal = iVal To 100 Step 1 
      bgw.ReportProgress(iVal) 
      Threading.Thread.Sleep(99) 
      If (bgw.CancellationPending = True) Then 
       e.Cancel = True 
       Exit For 
      End If 
     Next 
    End Sub 

    Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged 
     pbar.Value = e.ProgressPercentage 
     lblProgrss.Text = e.ProgressPercentage.ToString() & "%" 
    End Sub 

    Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted 

     If (e.Cancelled = True) Then 
      pic.Visible = False 
      pbar.Value = iVal 
      lblProgrss.Text = iVal & "%" 
      btnstart.Text = "Start" 
      btnstart.BackColor = Color.Green 
     Else 
      pic.Visible = False 
      btnstart.Text = "Start" 
      btnstart.BackColor = Color.Green 
      iVal = 0 
     End If 

    End Sub 

    Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstart.Click 
     If (btnstart.Text = "Start") Then 
      btnstart.Text = "Stop" 
      btnstart.BackColor = Color.Red 
      pic.Visible = True 
      bgw.RunWorkerAsync() 
     Else 
      If (bgw.IsBusy = True) Then 
       btnstart.Text = "Start" 
       btnstart.BackColor = Color.Green 
       bgw.CancelAsync() 
      End If 
     End If 
    End Sub 
End Class 
관련 문제