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.

kkandasaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LacutahCommented:
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.
0
kkandasaAuthor Commented:
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?

0
Corey ScheichDeveloperCommented:
>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
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

LacutahCommented:
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})
0
kkandasaAuthor Commented:
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.
0
LacutahCommented:
Time for me to do some reviewing too.... I don't get an opportunity to use delegates very often!
0
kkandasaAuthor Commented:
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.
0
Corey ScheichDeveloperCommented:
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
0
LacutahCommented:
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.
0
Corey ScheichDeveloperCommented:
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
0
kkandasaAuthor Commented:
Any final suggestions...?
0
LacutahCommented:
A delegate (argument) is required if you're going to use the Form.Invoke() method to force a function to run in the UI thread.

I have noticed that events raised by threads outside the UI, if pointed towards a sub that alters the UI, will often still run in the thread that raised the event instead of the UI thread.  

KKandasa - be careful!  Threads are scarry, but well-woven, can make for a beautifully responsive program!  Just remember two things:

1.  Never try to alter the UI from inside a thread other than the UI thread.
2.  Try not to ever share objects / values between two threads unless using syncloc or alternative functions so that two threads don't try to change / use the same object at the same time.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Corey ScheichDeveloperCommented:
I stand down I was unaware that events could run in the wrong thread.  Thank you for that Lacutah
0
kkandasaAuthor Commented:
Many thanks for the clarification. I will try out the delegate method and see what happens.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.