LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

control callback in differen threads

I have an application whith several panels. On one panel and its control callback I do a search in a file for specefic data.

This is performed by a while loop. The search might take quite some time.

The search is started on event COMMIT on the first panel control callback.

 

From another panel the search status is displayed (in % of how much data yet processed). This panel has an Escape button.

However, as the first callback has not yet terminated, the event inisiated by the escape button does not take place

until th first callback terminates.

 

How can I stop a started function (the while loop) from another panel/control ?

0 Kudos
Message 1 of 5
(3,970 Views)

First of all, you're probably not using separate threads - unless you've created a thread explicitly in your code, your cvi app is single threaded from your perspective (the CVI runtime can create threads for internal purposes).

 

Callbacks execute on the main thread (well, except for async timers) and run to completion in each callback before processing the next callback, unless you do something to allow events to be processed during the callback - and even then I'm not certain the the CVI runtime will enter a second callback if the first isn't returned but it might.  But someone from NI will answer that for you I'm sure.

 

Even if you were using multiple threads, you can't throw an exception from one thread into another in Win32 so you'd have to use some sort of flag / polling scheme to signal the callback to stop itself and return.

 

And you can't manipulate a gui object from any thread that didn't create/load it which can further complicate matters.

 

You could try having your escape control set an "abort search" flag that the search callback checks from time to time and quits searching if it's set.  Your search call back would have to allow event processing (ProcessSystemEvents function) to give the escape callback a chance to run.  This all presumes that CVI will enter a second callback before the first returns (and I think it does).

 

Menchar

Message Edited by menchar on 08-26-2009 10:56 AM
Message Edited by menchar on 08-26-2009 10:59 AM
0 Kudos
Message 2 of 5
(3,963 Views)

The simplest method to interrupt a while loop in progress may be to add a ProcessSystemEvents () inside the loop and test the value a global variable to exit from it: the global variable is raised by another button callback and ProcessSytemEvents () permits processing the UI events so that the second button callback is called during the execution of the while loop.

 

Having said that, I must warn you that such a structure is not the best that you can develop: the callback mechanism relies on callback functions to be *short* so that all the events can be handled in a proper way. An alternative solution could be to develop the callback that executes the search in such a way that it starts a separate thread that actually performs the search and can be interrupted when desired. CVI comes with a set of documents and examples that permits a good approach tomultithread development:I suggest you to carefully read Multithreading Overview (a PDF document located in <cvidir>\bin folder).  "Monitoring and Controlling Secondary Threads" chapter, in particular, gives you an explanation and some examples on how to stop a thread running.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 5
(3,957 Views)

Roberto already pointed you to ProcessSystemEvents().  Here's an example program I did a while back that shows the use of ProcessSystemEvents to allow a callback to run even when another is already running.  The instructions for the demo are on the UIR.

 

You can search this forum and the CVI on-line help for more details on ProcessSystemEvents().

0 Kudos
Message 4 of 5
(3,947 Views)

Thank you all.

I used ProcessSystemEvents ()  within the while loop and now it works.

I do agree with Roberto that in general callback should be fast, but in this application it does not matter.

 

0 Kudos
Message 5 of 5
(3,914 Views)