econnolly
Posts: 20
Joined: Wed Oct 04, 2006 10:39 pm
Location: USA
Contact: Website

Multi-Threaded example?

Fri Jun 12, 2009 1:53 am

Hi,

I've been using Bluetools for quite some time by calling events in a GUI thread, as shown in this piece of the SimpleService sample code:

Code: Select all

// Call events in GUI thread
m_manager.Parent = this;
// Call events in new thread (multi-threading)
// manager.Parent = null
But now I need to try calling events in a new thread - is there some sample code for this (in addition to uncommenting manager.Parent).

Thanks.

Econnolly

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: Multi-Threaded example?

Fri Jun 12, 2009 10:10 am

Manager.Parent synchronizes the events to the same thread as the object that is assigned to it.
If it's null the events are executed in the thread they are called from.

Regards,
Jonas

econnolly
Posts: 20
Joined: Wed Oct 04, 2006 10:39 pm
Location: USA
Contact: Website

RE: Multi-Threaded example?

Fri Jun 12, 2009 4:23 pm

Thanks for the response, Jonas. I'm still not sure how to set this up (as I say everything works fine using m_manager.Parent type code.)

The calling thread needs to handle Bluetools events?

My code has a GUI object, which handles events from a generic Instrument object, which handles events from Bluetools. The GUI object must be in a separate thread already, because if I don't use

m_manager.Parent = this

(where "this" is my GUI object), there are cross-thread errors. These errors happen when the GUI object handles data that:

1) starts in a Bluetools readCallback that raises an event to the
instrument object
2) is then sent by the instrument object in an event raised to the GUI object

What should be in a separate thread to handle the events from Bluetools? The instrument object?

Note that if I use

CheckForIllegalCrossThreadCalls = false;

these errors don't occur, but this is dangerous.

Econnolly

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: Multi-Threaded example?

Sat Jun 13, 2009 12:05 am

Any event that makes changes to your GUI must be synchronized.
This is not specific to BlueTools, it's just basic window programming.

If not all events make changes to the GUI you can synchronize them yourself by using the Invoke command in the event handler (also not BlueTools specific).

Regards,
Jonas

econnolly
Posts: 20
Joined: Wed Oct 04, 2006 10:39 pm
Location: USA
Contact: Website

RE: Multi-Threaded example?

Sat Jun 13, 2009 1:34 am

Originally posted by Jonas

Any event that makes changes to your GUI must be synchronized.
This is not specific to BlueTools, it's just basic window programming.

If not all events make changes to the GUI you can synchronize them yourself by using the Invoke command in the event handler (also not BlueTools specific).

Regards,
Jonas


Thanks, Jonas - I had started playing around with Invoke to deal better with the synchronization issues, so good to hear that is the right direction.

Econnolly

Return to “BlueTools”