How do you fire events from a multithreaded class?

I currently have a class that spawns different threads.  When certain things in those threads happen, I want it to fire off an event in the thread of the original "creator" of the class.  For instance, if the Form creates the object, all events should go to the Form's thread so it can make appropriate changes.

What I'm finding is that no matter what I do in the threads, I can NOT get the events to fire in the form without causing an error that the form's controls were created in a different thread.  Really need some help here, with a very clear explanation.
LVL 4
Javin007Asked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Okey dokey...here is a class that encapsulates a BackgroundWorker and raises a custom event.  Note that we do NOT have to do any manual marshalling with Invoke()/Delegates:
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mo As New MyOperation
        AddHandler mo.SomethingHasHappened, AddressOf MyOperation_SomethingHasHappened
    End Sub
 
    Private Sub MyOperation_SomethingHasHappened(ByVal msg As String, ByVal stamp As DateTime)
        Label1.Text = msg & " | " & stamp.ToString
    End Sub
 
End Class
 
Public Class MyOperation
 
    Public Event SomethingHasHappened(ByVal msg As String, ByVal stamp As DateTime)
 
    ' obviously you can have more of these things in your class...
    Private WithEvents bgw As New System.ComponentModel.BackgroundWorker
 
    Private counter As Integer
 
    Public Sub New()
        bgw.WorkerReportsProgress = True
        bgw.WorkerSupportsCancellation = True
        bgw.RunWorkerAsync()
    End Sub
 
    Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        ' backgroundworkers get killed automatically when the app shuts down...
        While True
            ' you can acess anything inside MyOperation from here to make decisions etc...
            System.Threading.Thread.Sleep(1000)
            counter = counter + 1
            bgw.ReportProgress(counter) ' this just makes ProgressChanged() fire below
        End While
    End Sub
 
    Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
        ' this is already marshaled to the UI thread because the MyOperation class was created on the UI thread
        ' make the main UI receive the event
        ' again, you can acess stuff inside MyOperation if need be, or pass things via "e.UserState" from the "worker"
        RaiseEvent SomethingHasHappened("Greeting #" & e.ProgressPercentage, DateTime.Now)
    End Sub
 
    Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
        ' you can raise a different event here...
    End Sub
 
End Class

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version VB.Net?....
0
 
Javin007Author Commented:
Express 2008.  
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It sounds like you are manually creating threads and not using Invoke()/Delegates to properly marshal your calls across threads.  I can show you how to do that if you like...but with VB.Net 2005 (and above) it's often easier just to use the BackgroundWorker() Control as it does this automatically for you in the ProgessChanged() and RunWorkerCompleted() events:
http://msdn.microsoft.com/en-us/library/8xs8549b.aspx
http://msdn.microsoft.com/en-us/library/hybbz6ke.aspx
0
 
Javin007Author Commented:
I did attempt to use Invoke/Delegates, but continued to get the same error when calling the delegated method, so obviously have that set up wrong.

The BackgroundWorker seems like a better option *IF* it can "talk" back to the calling class so that class can raise events.  From what I've read here, it seems like they expect the method to just run, and eventually stop.  This loop that will be threaded should run the entire time the app is running, and occasionally fire events.  (Long story short, I'm attempting to make a sockets wrapper class.)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can place an Infinite loop inside the "worker" method of the BackgroundWorker control.  The thread will be automatically killed when the app is closed.

The ReportProgress() method will cause the ProgressChanged() Event to fire:
http://msdn.microsoft.com/en-us/library/a3zbdb1t.aspx
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Do you need to see a simple example?...
0
 
Javin007Author Commented:
A small example would be awesome, thanks!
0
 
Javin007Author Commented:
Also keep in mind that this is a class, so not a form.  I'll need to create a BackgroundWorker object in code for it to be contained in the class.
0
 
Javin007Author Commented:
Perfect!  This is exactly what I needed.  Delegates/Invoke was entirely too complex.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.