Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cross Threading 1

Status
Not open for further replies.

BJZeak

Programmer
Joined
May 3, 2008
Messages
230
Location
CA
I have 2 vb modules created for the most part by VB express 2k8 ... Class Main is in main.vb and CLASS Partial Main is in main.designer.vb

I have one form in Main.vb [design] which is intended to be an instrument display with some pushbuttons and indicators ... I also have a Door which hides some of the buttons.

All of the Push Buttons and the Picture Box controls are defined in the CLASS Partial Main Class yet when I add a control event it is entered in the MAIN Class module

My pbDoor works fine opening and closing from a MAIN Class Function/Event but ALL of my indicators are giving me a cross threading error??? All I want to do is do a me.pbIndicator.visible = loState within a function ... it doesn't matter where I locate this function (in the MAIN class or the Partial MAIN Class) so I am Confused!!!

If the Thread that created the objects is PARTIAL MAIN CLASS then WHY is VB putting events in MAIN CLASS and WHY are both giving a cross thread error for all but one Control?
 
Ok I think I found the issue but still unclear on the how this is all fitting together.

There would have to be a class instance generated for the MAIN CLASS which initiates the Form and Form Controls ... this class instance is also creating a NEW serialport instance ... even though the Serial receive data event is registered within the MAIN CLASS instance it appears to have created a separate "THREAD"

I created a private sub updateIndicators which I called from the FORM_LOAD and also from the serialport_rx_data events ... the FORM_LOAD worked the SerialPort failed ... it failed because of the Cross Thread error ... looking at the local values this is because sender As System.Object and e As System.EventArgs are available from the MAIN CLASS code but NOT from the SerialPort event

Not sure I can access these objects in the serial event so what I finally did was add a timer that monitored a global flag set by the serial event ... the timer event can then call the display update code when the rx flag is set.

 
This might be off the subject. Just curious about the "Door." Can you provide some info on this?

Thanks,
Chilly442
 
The door is nothing more then a PictureBox control which when it covers (using me.pbDoor.visible = true) the button controls underneath, they are rendered unusable. I use a function ActionDoor(inState as Integer)

inState can be Open 1, Closed 2 or Toggle 3

Note all the controls underneath are also explictly set to hide() as well while the door is closed

The Door can currently be opened with a spacebar (when the app is active) or clicked on (hidden button)

The serial port is used to communicate to the remote instrument which relays Change of State indications of its Indicators ... plus allows for input from the Controls under the door ... the Cross Thread issue raised its head because even though I have created an instance of the SerialPort within CLASS MAIN it actually creates a separate thread under the SERIAL PORT CLASS ... As the Serial Port doesn't appear to have a pointer to what I consider the Parent Class (Main) it is causeing this Cross Thread error when I attempt to use a MAIN Function/Sub ... I was able to get around this by using a Global value and a Interval Timer in Main to service any changes. Not elegant but I am not looking for a saleable solution

I used to do VB/OOP and Windows programming back in the late 80's early 90's so this stuff isn't new but certainly has a few new twists I wasn't expecting.

THANX for your time.
 
The 3rd Faq on Cross Threading Faq796-6056 now makes sense to me ... the delegate process creates a set of pointers to the Threaded function/sub so it can be called cross thread.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top