2013-12-13 2 views
0

워크 북에 다양한 데이터를 출력하는 데 사용되는 ExcelOutput 클래스가 포함 된 BackgroundWorker이 있으며 바로 bw.WorkerSupportsCancellation = True이 설정되어 있음을 언급해야합니다.BackgroundWorker procss를 취소 할 수 없습니다

I가 Try/Catch를 사용 ExcelOutput의 오류를 검사하고있어, 출력의 각 단계에서

ErroReport()라는 함수를 사용하여 (에러 표시 필요한 경우. 오류 메시지와 함께

는, I는 취소 할 BackgroundWorker는이를 위해 내가 추가했습니다. ExcelOutput 클래스에 OutputWorker 속성을 추가 오류를 방지하고 나는이 bw_DoWork() 방법에 내 BackgroundWorker의 카피로 설정합니다.

그러나 ExcelOutput.ErroReport()에서 수행 취소가 가공용 아니다 응, 왜 그런지 모르겠다.

나는 bw.CancellationPending의 값을 테스트했으며 오류 발생 후 True으로 설정되었습니다. 또한 메시지 상자를 표시하여 If 조건이 작동하는지 테스트 한 결과 작동합니다. 웬일인지 Exit Sub 명령이 무시되는 것처럼 보인다.

누구든지 내가 뭘 잘못하고 있다고 제안 할 수 있습니까? 감사.

Private Sub ErrorReport(ByVal Ex As Exception, 
         Optional ByVal CustomMessage As String = "") 

    Call Me.ResetRange() ' Destroy the 'Range' object 
    Dim ErrorMessage As String = "Message: " & Ex.Message ' Set the default message 
    If CustomMessage <> "" Then ErrorMessage = CustomMessage & vbCrLf & vbCrLf & Ex.Message 
    Dim Result As Integer = MessageBox.Show(ErrorMessage, 
              "An Error Has Occured", 
              MessageBoxButtons.OK, 
              MessageBoxIcon.Stop) 

    '** Close the workbook (if it's open) and stop the OutputWorker *' 
    Try 
     Call Me.WB.Close(SaveChanges:=False) 
     If Me.OutputWorker.WorkerSupportsCancellation = True Then 
      Me.OutputWorker.CancelAsync() 
     End If 
    Catch 
    End Try 

End Sub 

답변

1

당신은 시도해야한다 -을

여기
Private Sub bw_DoWork(ByVal sender As Object, 
         ByVal e As DoWorkEventArgs) 

    Dim Excel As New ExcelOutput ' Create a new instance of the ExcelOutput class 
    Dim CurrentRow As Integer = 4 ' Set the first output row 

    '** Include a copy of the OutputWorker in the ExcelOutput (so that the OutputWorker can be cancelled) 
    Excel.OutputWorker = Me 

    If bw.CancellationPending = True Then 
     e.Cancel = True 
     Exit Sub 
    Else 
     Excel.Prepare() 
    End If 

    If bw.CancellationPending = True Then 
     e.Cancel = True 
     Exit Sub 
    Else 
     CurrentRow = Excel.OutputGroup("General", Headers, Data, 4) 
    End If 

    ' More stuff here... 

End Sub 

ExcelOutput 클래스에서 ErrorReport() 기능을 설정하는 방법 - 여기

BackgroundWorker 클래스에서 bw_DoWork() 기능을 설정하는 방법입니다 ErrorReport 함수에 매개 변수로 DoWorkEventsArgs를 추가하십시오.

Private Sub ErrorReport(ByVal Ex As Exception, 
        Optional ByVal CustomMessage As String = "", 
        ByVal e As DoWorkEventsArgs) 

Call Me.WB.Close(SaveChanges:=False) 
    If e.WorkerSupportsCancellation = True Then 
     e.CancelAsync() 
    End If 

배경 작업자를 취소 할 수 있습니다.

+0

고마워.하지만 같은 결과가 나왔어. –