Threading with events - raising events in the correct thread

Posted on 2009-02-23
Medium Priority
Last Modified: 2013-11-07

I've asked a couple of questions about threading in VB.NET recently as I'm very new to it, but there's one more thing that I can't figure out. My previous question (http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_24140554.html) related to raising events in the main thread when a background thread has completed. I got this working nicely with delegates and .Invoke(), but now I'm looking to encapsulate the bulk of my program in a class.

I want to be able to instantiate a class from from my main thread; it should be able to run some of its processes in a background thread, but raise events on the main thread. I can do this fairly easily by using delegates and passing a reference to my form into the class and using formReference.Invoke(), but ideally I want everything to do with threading to be handled within the class. The class should remain generic so that when it is reused, it just needs to be created WithEvents() rather than people having to mess about passing a reference to the creating form.

I've included some simple code and would greatly appreciate any help towards raising an event in my main thread. Apologies if I've butchered the lingo of multithreading (pretty sure I have) :P
Public Class Class1
    Public Event complete()
    Public Sub New()
        Dim backgroundThread As System.Threading.Thread
        backgroundThread = New System.Threading.Thread(AddressOf backgroundWorker)
    End Sub
    Private Sub backgroundWorker()
        RaiseEvent complete()
    End Sub
End Class

Open in new window

Question by:Corrup7ioN
1 Comment
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 23713896
Unless you write your class as a Component (dragged from the ToolBox onto your Form like a Timer control for instance), it will have no knowledge of what thread to invoke to unless you pass in something visible (like the Form).
See: http://msdn.microsoft.com/en-us/library/9hk12d4y.aspx

Without a component you have to either force the user to pass in a container or put the Invoke code out at the form level (which receives the events on a different thread).

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question