?
Solved

Multithreaded app and events.

Posted on 2005-03-29
14
Medium Priority
?
318 Views
Last Modified: 2010-04-23
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.

0
Comment
Question by:kkandasa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
14 Comments
 
LVL 9

Expert Comment

by:Lacutah
ID: 13653214
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
 

Author Comment

by:kkandasa
ID: 13655725
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
 
LVL 13

Expert Comment

by:Corey2
ID: 13656075
>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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:Lacutah
ID: 13656171
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
 

Author Comment

by:kkandasa
ID: 13656175
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
 
LVL 9

Expert Comment

by:Lacutah
ID: 13656203
Time for me to do some reviewing too.... I don't get an opportunity to use delegates very often!
0
 

Author Comment

by:kkandasa
ID: 13656585
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
 
LVL 13

Expert Comment

by:Corey2
ID: 13656631
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
 
LVL 9

Expert Comment

by:Lacutah
ID: 13657625
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
 
LVL 13

Expert Comment

by:Corey2
ID: 13671197
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
 

Author Comment

by:kkandasa
ID: 13675182
Any final suggestions...?
0
 
LVL 9

Accepted Solution

by:
Lacutah earned 1600 total points
ID: 13675549
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
 
LVL 13

Assisted Solution

by:Corey2
Corey2 earned 400 total points
ID: 13675575
I stand down I was unaware that events could run in the wrong thread.  Thank you for that Lacutah
0
 

Author Comment

by:kkandasa
ID: 13756720
Many thanks for the clarification. I will try out the delegate method and see what happens.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question