Corrup7ioN
asked on
Threading with events - raising events in the correct thread
Hi,
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 (https://www.experts-exchange.com/questions/24140554/Threading-with-Events-I'm-new-to-NET-and-rather-lost.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
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 (https://www.experts-exchange.com/questions/24140554/Threading-with-Events-I'm-new-to-NET-and-rather-lost.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)
backgroundThread.Start()
End Sub
Private Sub backgroundWorker()
System.Threading.Thread.CurrentThread.Sleep(3000)
RaiseEvent complete()
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.