Interrupting a while loop in a callback with another callback

2 visualizzazioni (ultimi 30 giorni)
Hello everyone.
I have a complex problem, but I'll try to simplify it. My GUI (using Matlab GUIDE) has 2 callback functions:
1.) Button1 - this button is used for running some calculations and reading data from server in while loop
2.) Button2 - this button is used for stopping those calculations and reading data from server
My code looks something like this
Callback function Button1 (Read data from server & run calculations):
function Button1_Callback(hObject, eventdata, handles)
% Do initialization for calcualtions
.
.
.
calculations_running = true; % Flag - I'm actually checking this not just setting it
% Update handles so other callbacks can use calculations_running
handles.calculations_running = calculations_running;
guidata(hObject, handles);
while (calculations_running == true)
% Do the calculations
.
.
.
% Read calculations_running from handles
calculations_running = handles.calculations_running;
end
Callback function Button2 (Stop calculations):
function Button2_Callback(hObject, eventdata, handles)
% Read calculations_running from handles
calculations_running = handles.calculations_running;
calculations_running = false
% Update handles
handles.calculations_running = calculations_running;
guidata(hObject, handles);
Of course, because I'm reading data from server I do create tcpip objects, open connection, checking for bytes and in the end flush tcpip buffers, close connections, delete unnecessary variables, etc. The main problem is when I hit Button2 I want to break out of the master while loop executed in Button1_Callback, but after i hit Button1, I enter master while loop in Button1_Callback. This while loop is blockable and Button2 doesn't react (so Button2_Callback won't execute and it won't set calculations_running = false, so my master while loop goes on forever). Any ideas how to solve this issue of while loop blocking my Button2_Callback?
P.S.: I was also thinking about possibly more suitable solution:
At the beggining (GUI initialization) calculations_running = false.
Button1_Callback would just initialize calculation parameters, connect to server for data reading and set calculations_running = true.
Calculations_Callback (a new callback function) would do the calculations as long as calculations_running == true.
Button2_Callback would close connection to server and set calculations_running = false.
Any ideas how to do that? I'm not sure if you can configure callback functions (i.e. Calculations_Callback) to execute until some global condition is met (i.e. calculations_running == true).
Thank you.
Leon

Risposte (1)

Geoff Hayes
Geoff Hayes il 3 Feb 2017
Leon - a problem with your while loop (I'm guessing since not all the code is shown) is that it is "tight" and so is not "interruptible". So any other action (like pressing the second push button) will wait until the the loop has completed...which will never happen.
You can make your while loop interruptible by including a short pause at the end (or beginning) of the loop to give other actions a chance to execute. See callback sequencing and interruption for details. For example,
while (calculations_running == true)
% Do the calculations
.
.
.
% get the latest version of handles
handles = guidata(hObject);
% Read calculations_running from handles
calculations_running = handles.calculations_running;
% short pause so that this loop can be interrupted
pause(0.01);
end
Note how we have to refresh the handles structure as the one that you started off with is only a copy and so any update to it from the other push button callback would not appear.
The above should work but you may want to consider using a timer to read the data from the server and do some calculations. You would start the timer from within the first push button, the timer callback would query the server and do some calculations, and the second push button would stop the timer. See http://www.mathworks.com/matlabcentral/answers/178682-collecting-a-variable-every-two-minutes for an example of using a timer within a GUI which you should be able to modify for your needs.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by