2016-07-25 47 views
0

您好再次StackOverflow社區!VB.Net BackgroundWorker和AsyncCancel無法正常工作

我遇到了BackgroundWorker和AsyncCancel的一些問題。 BackgroundWorker只發送一封電子郵件,但我希望能夠報告何時發送任務或電子郵件,並且能夠取消發送的任務或電子郵件。

問題是點擊取消後,它繼續,然後報告錯誤,沒有取消。

任何幫助,非常感謝!

謝謝!

這裏是我完整的代碼減去意見和進口:

Private Sub Sendmail_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 

    StatusLabel.Text &= "Idle" 

End Sub 

Private Sub SendmailBackgroundWorker_DoWork(sender As Object, e As DoWorkEventArgs) Handles SendmailBackgroundWorker.DoWork 

    Try 

     Dim Smtp As New SmtpClient() 
     Dim Email As New MailMessage() 

     Smtp.Port = 25 
     Smtp.Host = "mail.server.com" 

     Smtp.EnableSsl = False 
     Smtp.UseDefaultCredentials = False 

     Smtp.Credentials = New Net.NetworkCredential("[email protected]", "password") 

     Email = New MailMessage() 
     Email.From = New MailAddress(FromTextBox.Text) 
     Email.To.Add(ToTextBox.Text) 
     Email.Subject = SubjectTextBox.Text 
     Email.IsBodyHtml = False 
     Email.Body = BodyTextBox.Text 

     Smtp.Send(Email) 

    Catch ex As Exception 

     MsgBox("Sendmail Error!" & vbNewLine & vbNewLine & ex.ToString) 

    End Try 

    If SendmailBackgroundWorker.CancellationPending Then 

     StatusLabel.Text = "Canceling" 
     e.Cancel = True 

    End If 

End Sub 

Private Sub SendmailBackgroundWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles SendmailBackgroundWorker.RunWorkerCompleted 

    StatusLabel.Text = "Status: " 

    If (e.Error IsNot Nothing) Then 


     StatusLabel.Text &= "Worker Error!" & vbNewLine & vbNewLine & e.Error.Message 

    ElseIf e.Cancelled Then 

     StatusLabel.Text &= "Canceled!" 

    Else 

     StatusLabel.Text &= "Sent!" 

    End If 

    SendButton.Enabled = True 
    CancelButton.Enabled = False 

End Sub 

Private Sub SendButton_Click(sender As Object, e As EventArgs) Handles SendButton.Click 

    StatusLabel.Text = "Status: " 

    SendButton.Enabled = False 
    CancelButton.Enabled = True 

    SendmailBackgroundWorker.WorkerSupportsCancellation = True 
    SendmailBackgroundWorker.WorkerReportsProgress = True 

    StatusLabel.Text &= "Sending..." 

    SendmailBackgroundWorker.RunWorkerAsync() 

End Sub 

Private Sub CancelButton_Click(sender As Object, e As EventArgs) Handles CancelButton.Click 

    CancelButton.Enabled = False 
    SendmailBackgroundWorker.CancelAsync() 

End Sub 
+0

您擁有的另一個問題是發送郵件可能非常快。這可能是用戶甚至沒有時間取消發送。 – Enigmativity

回答

1

即工作,正是因爲它應該。問題是你沒有正確地閱讀它的工作原理。在BackgroundWorker上撥打CancelAsync(NOT​​)不會取消任何操作。它所做的就是在BackgroundWorker對象上設置一個標誌。您需要在您的DoWork事件處理程序中測試該標誌,並且如果已設置,那麼您需要停止工作。在您當前的代碼中,您只有在電子郵件發送完畢之後纔會測試該標誌,因此無論您是否嘗試取消電子郵件,它都會被髮送。

您高估什麼取消BackgroundWorker可以完成。 BackgroundWorker本身並不知道您在DoWork事件處理程序中所做的事情,因此它不會簡單地中止它。它使您有機會在代碼中的適當位置終止任務。如果沒有適當的點,那麼你不能取消任何事情。

在你的情況下,一旦你在你的SmtpClient上撥打Send,你不能做任何事情,直到該同步方法返回,所以你不能取消它。你應該做的是根本不使用BackgroundWorker,而是內置於SmtpClient類中的異步功能。它有一個SendAsync方法和一個SendAsyncCancel方法,所以你可以讓它爲你處理多線程。

+0

感謝您的幫助Enigmativity和jimcilhinney,我會在早上看到這一點。 –