函数运行后将表单发送回去

时间:2014-06-25 17:07:31

标签: vb.net backgroundworker

我已经获得了以下代码部分:

Public Sub SendTestEmail()
    Try
        Dim Mail As New MailMessage

        Mail.Subject = "Test email"
        Mail.To.Add(smtpTXTsendto.Text)

        Mail.From = New MailAddress(smtpTXTusername.Text)
        Mail.Body = "This is a test message"


        Dim SMTP As New SmtpClient(smtpTXTserver.Text)
        If smtpCHECKssl.Checked = True Then
            SMTP.EnableSsl = True
        Else
            SMTP.EnableSsl = False
        End If
        SMTP.Credentials = New System.Net.NetworkCredential(smtpTXTusername.Text, smtpTXTpassword.Text)
        SMTP.Port = smtpTXTport.Text
        SMTP.Send(Mail)
        MessageBox.Show("A test email has been sent." & Environment.NewLine & Environment.NewLine & "To: " & smtpTXTsendto.Text & Environment.NewLine & "From: " & smtpTXTusername.Text & "." & Environment.NewLine & Environment.NewLine & "If you did not recieve an email, please check your settings and try again.", "Test Email")
    Catch ex1 As Exception
        MessageBox.Show(ex1.Message)
        Return
    End Try
End Sub

在背景工作者内部调用Sub SendTestEmail

我遇到了一个奇怪的问题,就像MessageBox出现时一样,我单击OK表单会被发送到屏幕后面,在所有应用程序后面...... < / p>

我已尝试添加Me.focus,但它为我提供了有关违反交叉广告的问题。

为什么会发生这种情况?

后台工作人员代码:

Private Sub BGWSendTestEmail_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGWSendTestEmail.DoWork
    SendTestEmail()
End Sub
Private Sub BGWSendTestEmail_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGWSendTestEmail.RunWorkerCompleted
    If (e.Cancelled) Then
        MsgBox("Something went wrong!")
    Else
        GroupBoxTesting.Visible = False
        Me.Enabled = True
    End If
End Sub
Private Sub SMTPButtonTest_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMTPButtonTest.Click
    GroupBoxTesting.Visible = True
    Me.Enabled = False
    BGWSendTestEmail.RunWorkerAsync()
End Sub

1 个答案:

答案 0 :(得分:1)

将BGWSendTestEmail_RunWorkerCompleted更改为类似的内容并从SendTestEmail中删除异常处理

Me.Enabled = True ' Always enable form first after completed

If e.Error Is Nothing Then

            If Not e.Cancelled Then
                MessageBox.Show("A test email has been sent." _
                                & Environment.NewLine _
                                & Environment.NewLine _
                                & "To: " & smtpTXTsendto.Text _
                                & Environment.NewLine _
                                & "From: " & smtpTXTusername.Text _
                                & "." & Environment.NewLine _
                                & Environment.NewLine _
                                & "If you did not recieve an email, please check your settings and try again.", _
                                "Test Email")

                GroupBoxTesting.Visible = False 'Maybe put this also into start of method?
            Else
                MsgBox("Something went wrong!")
            End If
Else
            MessageBox.Show("Email sending failed. Exception: " & e.Error.Message)
End If

如果您调试此程序,您的调试器将在do_work方法中附加异常,但如果您在没有调试器的情况下运行异常,则会在完成的方法中处理异常。