Results 1 to 4 of 4

Thread: [RESOLVED] [2008] Backgroundworker problems!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Resolved [RESOLVED] [2008] Backgroundworker problems!

    Regarding the following code:

    Vb.net Code:
    1. Private Sub bgw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
    2.         Try
    3.             Dim testEmail As New NetEmail("[email protected]", TextBox4.Text.Trim, TextBox5.Text.Trim, TextBox6.Text.Trim, TextBox7.Text.Trim)
    4.             testEmail.AddEmail("[email protected]")
    5.             testEmail.Timeout = 10000
    6.             testEmail.EnableSSL = CheckBox1.Checked
    7.             testEmail.AddMessage("Test message")
    8.             testEmail.Send("[email protected]", "Test Email")
    9.             btnNext.Enabled = True
    10.             bgw.ReportProgress(100)
    11.         Catch ex As Exception
    12.             bgw.ReportProgress(0, ex)
    13.             btnNext.Enabled = False
    14.         End Try
    15.     End Sub
    16.  
    17.     Private Sub bgw_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
    18.         If e.ProgressPercentage = 0 Then
    19.             Dim CaughtEx As Exception = DirectCast(e.UserState, Exception)
    20.             MessageBox.Show(CaughtEx.Message)
    21.             MessageBox.Show("Your email settings were not configured")
    22.         ElseIf e.ProgressPercentage = 100 Then
    23.             MessageBox.Show("Test Successfull")
    24.         End If
    25.     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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Structure EmailData
    2.     Public Str1 As String
    3.     Public Str2 As String
    4.     Public Str3 As String
    5.     Public Str4 As String
    6.     Public EnableSSL As Boolean
    7. End Structure
    8.  
    9. Private Sub bgw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
    10.     Dim data As EmailData
    11.  
    12.     If TypeOf e.Argument Is EmailData Then
    13.         data = DirectCast(e.Argument, EmailData)
    14.     End If
    15.  
    16.     Try
    17.         Dim testEmail As New NetEmail("[email protected]", Data.Str1, Data.Str2, Data.Str3, Data.Str4)
    18.         testEmail.AddEmail("[email protected]")
    19.         testEmail.Timeout = 10000
    20.         testEmail.EnableSSL = Data.EnableSSL
    21.         testEmail.AddMessage("Test message")
    22.         testEmail.Send("[email protected]", "Test Email")
    23.  
    24.         'Success
    25.         e.Result = Nothing
    26.     Catch ex As Exception
    27.         'Failure
    28.         e.Result = ex
    29.     End Try
    30. End Sub
    31.  
    32. Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
    33.     Dim err As Exception = TryCast(e.Result, Exception)
    34.  
    35.     If err Is Nothing Then
    36.         'Success
    37.         btnNext.Enabled = True
    38.         MessageBox.Show("Test Successfull")
    39.     Else
    40.         'Failure
    41.         btnNext.Enabled = False
    42.         MessageBox.Show(err.Message)
    43.         MessageBox.Show("Your email settings were not configured")
    44.     End If
    45. 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:
    1. Dim data As EmailData
    2.  
    3. With data
    4.     .Str1 = Me.TextBox4.Text.Trim()
    5.     .Str1 = Me.TextBox5.Text.Trim()
    6.     .Str1 = Me.TextBox6.Text.Trim()
    7.     .Str1 = Me.TextBox7.Text.Trim()
    8.     .EnableSSL = Me.CheckBox1.Checked
    9. End With
    10.  
    11. Me.bgw.RunWorkerAsync(data)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width