kkandasa
asked on
Multithreaded app and events.
Hello,
I have written a multithreaded VB app with the following structure.
The main thread (Thread A) has a set of display screens.
Another thread (Thread B) communicates via TCP/IP with an external server to get information updates that it stores in a global array.
The 3rd Thread (Thread C) updates the info display screens with info from the gloabl array.
I need to handle a scenario whereby Thread B loses communication with the external server. The requirement is to pop up a in the main screen and also close releveant display screens. What is the best way to do this?
Is it possible to close screens/controls in Thread A from Thread C.? If not, should I fire an event from Thread C.? How do i ensure the event is consumed by thread A..?
Many thanks.
I have written a multithreaded VB app with the following structure.
The main thread (Thread A) has a set of display screens.
Another thread (Thread B) communicates via TCP/IP with an external server to get information updates that it stores in a global array.
The 3rd Thread (Thread C) updates the info display screens with info from the gloabl array.
I need to handle a scenario whereby Thread B loses communication with the external server. The requirement is to pop up a in the main screen and also close releveant display screens. What is the best way to do this?
Is it possible to close screens/controls in Thread A from Thread C.? If not, should I fire an event from Thread C.? How do i ensure the event is consumed by thread A..?
Many thanks.
ASKER
Sorry, I meant to ask about firing an event from Thread B to A (Not C to A) .
But I understand your point on the issues about affecting user controls from a secondary thread. I found out that I can update information on the user control from a secondary thread (what thread C does), but anytime a control is created from a secondary thread, an exception occurs.
I do need some more info as I am not sure what you mean by delegate as I have never used them before. Is it possible to fire an event from Thread B and have it consumed and acted on by Thread A?
But I understand your point on the issues about affecting user controls from a secondary thread. I found out that I can update information on the user control from a secondary thread (what thread C does), but anytime a control is created from a secondary thread, an exception occurs.
I do need some more info as I am not sure what you mean by delegate as I have never used them before. Is it possible to fire an event from Thread B and have it consumed and acted on by Thread A?
>I do need some more info as I am not sure what you mean by delegate as I have never used
>them before. Is it possible to fire an event from Thread B and have it consumed and acted
>on by Thread A?
Sure as long as the object that references the thread is still live.
'in the class declarations
public withevents Thread1 as thread
'......other code to setup a thread
thread1 = new thread (addressof Class1.Threadsub)
thread1.start
end sub
sub CatchEvent1 () handles (Thread1.Threadevent1)
msgbox ("The thread has started")
end sub
sub CatchEvent2 () handles (Thread1.Threadevent2)
msgbox ("The thread is Ending")
end sub
end class
Public Class Class1
PublicEvent ThreadEvent1 ()
PublicEvent ThreadEvent2 ()
Public Sub ThreadSub ()
raisevent threadevent1 ()
'.......do something
msgbox("The thread is doing something"
Raisevent Threadevent2()
end sub
end class
regards
Corey2
>them before. Is it possible to fire an event from Thread B and have it consumed and acted
>on by Thread A?
Sure as long as the object that references the thread is still live.
'in the class declarations
public withevents Thread1 as thread
'......other code to setup a thread
thread1 = new thread (addressof Class1.Threadsub)
thread1.start
end sub
sub CatchEvent1 () handles (Thread1.Threadevent1)
msgbox ("The thread has started")
end sub
sub CatchEvent2 () handles (Thread1.Threadevent2)
msgbox ("The thread is Ending")
end sub
end class
Public Class Class1
PublicEvent ThreadEvent1 ()
PublicEvent ThreadEvent2 ()
Public Sub ThreadSub ()
raisevent threadevent1 ()
'.......do something
msgbox("The thread is doing something"
Raisevent Threadevent2()
end sub
end class
regards
Corey2
In your main class, pretend you have a sub called DataHas Changed and a delegate that points to it:
Friend Delegate Sub DataChanged(ByVal NewData As Integer)
Friend d_DataChanged As New DataChanged(AddressOf DataHasChanged)
Friend Sub DataHasChanged(ByVal i As Integer)
... This handles Thred B's state changing, updates user controls, etc.
End Sub
From Thread B, you would call (with a value of 25):
FormWhateverName.Invoke(Fo rmWhatever Name.d_Dat aChanged, new Object() {25})
Friend Delegate Sub DataChanged(ByVal NewData As Integer)
Friend d_DataChanged As New DataChanged(AddressOf DataHasChanged)
Friend Sub DataHasChanged(ByVal i As Integer)
... This handles Thred B's state changing, updates user controls, etc.
End Sub
From Thread B, you would call (with a value of 25):
FormWhateverName.Invoke(Fo
ASKER
I also read up a little on delegates.. I guess it acts as a function pointer. What is the main difference between events and delegates and what is the best to use for my scenario?
thanks.
thanks.
Time for me to do some reviewing too.... I don't get an opportunity to use delegates very often!
ASKER
Lacutah,
I actually need to close the relevant display form (created in the main thread) upon thread B losing connectivity with the external server. I assume you can't do this from Thread B code space itself..thus the need for events or delegates..?
Thanks.
I actually need to close the relevant display form (created in the main thread) upon thread B losing connectivity with the external server. I assume you can't do this from Thread B code space itself..thus the need for events or delegates..?
Thanks.
Could you have form B start and watch the thread since they are associative and have form B close itself upon the close event of your thread.
Corey2
Corey2
If you're interacting with the user interface, then you will need to use a delegate from any outside threads to invoke code on the user interface thread.
Lacutah,
Isn't the purpose of delegates to allow the access to events of an object declared in a different class. If the OP declares the Object the thread will run from, and the thread in the class for form B they can then catch any events from that object in form B causing it to close upon a LostConnection Event. This would make delegates unnecessary. Though I am sure the same could be done from Form A using delegates.
Corey2
Isn't the purpose of delegates to allow the access to events of an object declared in a different class. If the OP declares the Object the thread will run from, and the thread in the class for form B they can then catch any events from that object in form B causing it to close upon a LostConnection Event. This would make delegates unnecessary. Though I am sure the same could be done from Form A using delegates.
Corey2
ASKER
Any final suggestions...?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks for the clarification. I will try out the delegate method and see what happens.
You'll need to create a delegate on the form to handle the call from thread c, then call it from thread c using the form's invoke method. You can write your delegate to handle as many arguments as you want to pass.