Hi folks,
Just want to double check my understanding is correct...
Got a windows service that fires of a series of scheduled tasks. My ScheduledTask base class has an event called TaskComplete which also passes back the result of the task (ie success/fail) The idea was so that we can start the task and then just listen for the TaskComplete event to be fired, however...
If the windowsservice launches the ScheduledTasks Start method on a seperate thread, If my understanding is correct, the TaskComplete event will not be picked up by the windowsservice that called it (because its on a seperate thread.)
Now I know that for the senario above, I could just use the provided background worker and its provided methods but I need to know if my understanding of threading is correct. In other words, if I decide to add other events at a later date, can these events be picked up by a different thread or are they thread specific.
'Using reflection to get the class....
objTask =
DirectCast(objClass, ScheduledTask) AddHandler objTask.TaskComplete, AddressOf OnScheduledTaskComplete ' create our thread and pass our method in as the address Dim backThread As New Threading.Thread(AddressOf objTask.StartTask)backThread.Start()

Threading and Events
Don Higgins
Could someone PLEASE dumb-down ONE answer about threading Pretend you are teaching a class and you might just stop a million of these questions on threading
If someone could walk through and explain what threading does, why you need it, what each piece of code that you offer as a solution does, etc... I know I would appreciate the heck outta it. Heck a flow-chart that maps out a threaded call to a control would be invaluable as well...
Just my $0.02 worth...
Jef Patat
MunishGupta
Ok...Threading is a very broad topic that will not be explained in a single thread...However for simplistics sake...
Think of a thread as the 'highway' that your application uses to send code and data (the cars) to and from the processor. The user interface uses a single thread that is used to present the user with information. If you use that same thread to do intesive work (Mac Trucks) all of the cars will have to pull over and wait for the Mac trucks to get done hauling there loads!
In other words the user interface freezes and your user thinks that the application has locked up....so how do we stop that...we create a seperate highway for the Mac trucks to travel on! Now while the Mac trucks are going down thier highway the cars can safely travel back and forth down thier own highway!
There are several other reasons for using threads ..but the principle is the same..you want more than one thing going on at the same time without bogging each other down...for more information on threading start with
Threads and Threading : http://msdn2.microsoft.com/en-us/library/6kac2kdh.aspx
And check out the Threading Namespace:
http://msdn2.microsoft.com/en-us/library/system.threading.aspx
Captain Jack
Threading explanations should really start with the basics... You may know this already but its always good to restate this fact.. a single cpu can only do one thing at once.
Now when you run windows, you may have lots of applications open at the same time and they all appear to be running at the same time, in other words, windows gives the appearance that your computer is doing multiple tasks all at the same time. In actual fact, windows is only ever doing one thing at once but the OS handles each process/application/thread and allocates CPU time to each one in turn.
"So if a cpu can only do one thing (or thread) at once, why would you want to create a multithreaded application " I hear you ask...
Go back to the windows OS. Each thread is given a level of importance and the UI always has the highest level of importance. This should mean that the UI always appears responsive but....
Have you ever used say scanning software/imaging software when the UI freezes during the scanning process This is because the scanning is running on the same thread as the UI so the UI has to wait until the scanning is complete before you can interact with it again.
Basically this means that if you have a task or method that is called by the UI that takes say 10 seconds to complete, unless you create a new thread (or execution path) for this task to run on, your application will appear to freeze for 10 seconds. This can be very annoying for the user and as another example, lets say you clicked scan by mistake and you wanted to cancel the task. If the application is not multithreaded (splitting the UI from the worker) you would not be able to cancel the task as the UI you need to interact with is frozen due to the fact that the thread is waiting for the scanning to complete. If you delegate the scanning process to another thread, your UI is always free to respond to your input. This way you can click a Cancel button on the UI and the UI's thread can then ask the scanning thread to stop. Make sense
One important thing to remember is that CPU manufacturers are all pushing dual and quad core CPU's and as a result, there is a lot of power out there to unleash with multithreaded applications!
SQLServer2050
It is possible to setup cross thread event handling...just need to use some delegates and invoking
From my post in thread: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=805086&SiteID=1
From event on same thread
Public Class MainDisplayForm
Private WithEvents PFFA As New PriceFeedFromAPI
Private Sub SetText(ByVal TheText As String) Handles PFFA.TextChangedEvent
Me.TextBox1.Text = TheText
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
PFFA.Test()
End Sub
End Class
Basic setup of event
Public Class PriceFeedFromAPI
Public Event TextChangedEvent(ByVal TheText As String)
Private Sub FireTextChangedEvent()
RaiseEvent TextChangedEvent("This is the text from the event")
End Sub
Public Sub Test()
FireTextChangedEvent()
End Sub
End Class
Using separate thread with delegate for cross thread safe call…
Public Class MainDisplayForm
Private MyThread As System.Threading.Thread = Nothing
Delegate Sub SetTextCallback(ByVal [text] As String)
Private Sub TestThread()
Dim PFFA As New PriceFeedFromAPI
AddHandler PFFA.TextChangedEvent, AddressOf SetText
PFFA.Test()
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.MyThread = New Thread(New ThreadStart(AddressOf Me.TestThread))
Me.MyThread.Start()
End Sub
Private Sub SetText(ByVal [text] As String)
If Me.TextBox1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.TextBox1.Text = [text]
End If
End Sub
End Class
Event is fired on the MyThread and handled in the UI thread