LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Can you write to the GUI main panel from an Async Timer thread?

I currently am using an Async Timer (created with function NewAsyncTimer) to collect data. I am using the Asynch timer thread to also plot a Strip Chart from the data and then print out the data to a numeric readback box on the main gui screen. It seems to be working but it has come to my attention that there are typically problems in MS Windows when one attempts to modify the GUI through a thread other than the thread in which the GUI is running. I am using Windows XP.
0 Kudos
Message 1 of 5
(3,620 Views)
I have done some searching on MSDN and google and didn't come across any recommendation against updating a GUI from another thread. Of course, you have to be careful about race conditions and maintain correct data at specific points in the program. In CVI, you could always use the PostDeferredCallToThread function to post a call to the UI thread. Then inside the "call", you can do all your user interface updating.

Hope that helps.
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 2 of 5
(3,597 Views)
If you are using the native CVI controls, the CVI runtime takes care of this for you. Unless specified otherwise, all the CVI libraries are multi-thread safe.

With typical Win32 UI controls (using ActiveX for example), you need to ensure that you don't access the UI component from a thread other than the one that created the UI component in the first place, unless ofcourse this is explicitly supported by the UI control. So if you are using ActiveX UI controls with a multihtreaded CVI application, you will need to be aware of this. CVI does provide an extensive multi-threading library that includes API for data protection ( as Wendy mentioned). This API allows you to easily create thread safe queues, thread safe variables and so on.

For information about Multithreading and CVI user interfacs, check out the CVI help under Contents >> Library Reference >> User Interface Library >> Multithreading. This is an excellant resource that will answer all your questions about creating multi threaded applications with user interfaces.

I hope this clears things.

Message Edited by bilalD on 04-20-2005 09:57 AM

Bilal Durrani
NI
0 Kudos
Message 3 of 5
(3,574 Views)
Hi.

Bilal, although "...all the CVI libraries are multi-thread safe", is there not a significant performance hit if the UI is updated from another thread? When I moved all of my UI updates to the main thread, I found that the load on the CPU(s) was substantially reduced.

Also, I would strongly advise ejv11 to use PostDeferredCall() or PostDeferredCallToThread(), as Wendy suggests, to move any time-consuming operations out of his async timer callback functions; otherwise the callback queue could become unmanageable.

(Please correct me if I'm wrong)

Regards,
Colin.
0 Kudos
Message 4 of 5
(3,562 Views)
I am not aware of significant performance issues in updating the UI from another thread, besides the small overhead of synchronization objects being used to avoid data corruption. There are certain panel and UI attributes that you cannot access from non UI threads (cannot discard a panel from a thread other than the one that it was created in), but these behaviors are documented in the help topics I referred to previously. Andthese are not performance related.

The locks will not be used if all the updating was being done in the UI thread, so it might be a little faster (no waiting to get access to critical sections). But I have not seen significant deterioration in application performance occur from cross-thread calls to UI controls.

If you have come across some cases where the above is not true, let me know and I can investigate it some more.
Bilal Durrani
NI
0 Kudos
Message 5 of 5
(3,538 Views)