|
-
Mar 15th, 2008, 03:34 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2008] Backgroundworker problems!
Regarding the following code:
Vb.net Code:
Private Sub bgw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
Try
Dim testEmail As New NetEmail(" [email protected]", TextBox4.Text.Trim, TextBox5.Text.Trim, TextBox6.Text.Trim, TextBox7.Text.Trim)
testEmail.Timeout = 10000
testEmail.EnableSSL = CheckBox1.Checked
testEmail.AddMessage("Test message")
btnNext.Enabled = True
bgw.ReportProgress(100)
Catch ex As Exception
bgw.ReportProgress(0, ex)
btnNext.Enabled = False
End Try
End Sub
Private Sub bgw_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
If e.ProgressPercentage = 0 Then
Dim CaughtEx As Exception = DirectCast(e.UserState, Exception)
MessageBox.Show(CaughtEx.Message)
MessageBox.Show("Your email settings were not configured")
ElseIf e.ProgressPercentage = 100 Then
MessageBox.Show("Test Successfull")
End If
End Sub
if an exception is thrown, the first messagebox is showing the the Exception message. The second messagebox Is NOT SHOWN.
if it is succesfull THE MESSAGE BOX is still NOT SHOWN?
does anyone know whatsup?
-
Mar 15th, 2008, 09:56 PM
#2
Re: [2008] Backgroundworker problems!
You're going about that the wrong way. First of all, you're performing illegal cross-thread operations from your DoWork event handler. Secondly, you're using the ProgressChanged event to notify the user of the final result. That's what the RunWorkerCompleted event is for. You should restructure your code considerably:
vb.net Code:
Private Structure EmailData Public Str1 As String Public Str2 As String Public Str3 As String Public Str4 As String Public EnableSSL As Boolean End Structure Private Sub bgw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork Dim data As EmailData If TypeOf e.Argument Is EmailData Then data = DirectCast(e.Argument, EmailData) End If Try Dim testEmail As New NetEmail(" [email protected]", Data.Str1, Data.Str2, Data.Str3, Data.Str4) testEmail.Timeout = 10000 testEmail.EnableSSL = Data.EnableSSL testEmail.AddMessage("Test message") 'Success e.Result = Nothing Catch ex As Exception 'Failure e.Result = ex End Try End Sub Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted Dim err As Exception = TryCast(e.Result, Exception) If err Is Nothing Then 'Success btnNext.Enabled = True MessageBox.Show("Test Successfull") Else 'Failure btnNext.Enabled = False MessageBox.Show(err.Message) MessageBox.Show("Your email settings were not configured") End If End Sub
Now you're using the event that is specifically intended to notify you that the background operation is complete to notify you that the background operation is complete.
You're also not accessing any controls in the background thread, as is appropriate. When you call RunWorkerAsync on the BackgroundWorker you need to create an EmailData object and pass it in:
vb.net Code:
Dim data As EmailData With data .Str1 = Me.TextBox4.Text.Trim() .Str1 = Me.TextBox5.Text.Trim() .Str1 = Me.TextBox6.Text.Trim() .Str1 = Me.TextBox7.Text.Trim() .EnableSSL = Me.CheckBox1.Checked End With Me.bgw.RunWorkerAsync(data)
-
Mar 15th, 2008, 10:36 PM
#3
Thread Starter
Fanatic Member
Re: [2008] Backgroundworker problems!
Hey man thanks a lot..
I didnt even know there was a e.result.
By the way, apart from the illegal cross thread operations, why didnt my code work? Liek how come only the first messagebox showed, but not the second?
Edit: I can't even give you more repuatation cuz I have to spread it around! lol
-
Mar 15th, 2008, 11:00 PM
#4
Re: [2008] Backgroundworker problems!
I'm not sure what was causing that symptom in your original code but I'm afraid I don't care to investigate given that the design was inappropriate anyway.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|