Threading with events - raising events in the correct thread

Posted on 2009-02-23
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 ( 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 85

    Accepted Solution

    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).

    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).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now