Link to home
Start Free TrialLog in
Avatar of kkandasa
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.

Avatar of Lacutah
Lacutah
Flag of United States of America image

What you want to use is the invoke statement for the form (visual interface) from thread c.  All user controls are run under one thread, and executing them from a different thread can cause very bad things to happen.

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.
Avatar of kkandasa
kkandasa

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?

>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
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(FormWhateverName.d_DataChanged, new Object() {25})
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.
Time for me to do some reviewing too.... I don't get an opportunity to use delegates very often!
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.
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
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
Any final suggestions...?
ASKER CERTIFIED SOLUTION
Avatar of Lacutah
Lacutah
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many thanks for the clarification. I will try out the delegate method and see what happens.