Pause() function is screwing up control flow in my GUI

Hi there!
I'm having some problems with the pause() function in a GUI that I've been working on. I realize that pause() has some known issues, so I'm hoping that someone will be familiar with the symptoms I'm seeing and know of a workaround. I'll try and describe my program as simply as possible, but I can add more detail if this is too vague.
The GUI has a "start" button whose callback includes a while loop that runs until the "stop" button is pressed. At the beginning of each loop iteration, there's a pause(0.02) to reduce the GUI's CPU load. During each iteration, the loop calls various functions to check on different things. If it doesn't like what it sees, it calls other functions, some of which include pause commands.
The problem I'm seeing is that the GUI doesn't seem to flow in a linear fashion, and I think that the pauses are to blame. For example, in the main while loop, there's an
if [function call 1] [function call 2, which includes a pause command] end
... and it seems that when function 1 returns true, function 1 still gets called many more times than function 2.
So - does this vague explanation ring any bells for anyone? Is there another function that I could use instead of pause() (supplemented by some "drawnow"s)? Or do I need to include a code sample to make this more comprehensible?
Thanks very much for your help.

1 Comment

I wouldn't expect that unless you're using a parfor instead of a for.

Sign in to comment.

Answers (1)

"At the beginning of each loop iteration, there's a pause(0.02) to reduce the GUI's CPU load." This doesn't make sense to me. If you want to reduce the CPU load, you need to get rid of pause() so the time can be used to execute other code.
My experience on the need of using pause() in my GUI application is when there is a dialog, such as uigetfile() or msgbox(). After the user clicks the button, if there is intense calculation right after, the GUI usually freezes and cause problem. I found that adding a pause(0.1) solves the problem. drawnow() actually won't work as good as pause().
Is the pause() statement inside or outside the while-loop? If it's outside the while-loop, it sounds familiar to me as I assume the while-loop is CUP intense.
You'll probably need to show some code to let others get better idea.

4 Comments

First, a proviso - I am not a professional programmer.
This GUI is meant to run continuously and monitor various processes. Since it doesn't have an "end goal", I inserted a pause in the main loop so that it isn't constantly doing calculations and wasting processor cycles. This does seem to have had a positive impact on the GUI's performance.
I'll try to give you a clearer sense of what's going on in my code, but I'll have to leave out a lot of details. First of all, there's a timer that runs in the background when the GUI is launched that updates the GUI's uicontrols based on the current state of the machine that it's monitoring.
Here's a very stripped-down version of the main while loop, which is part of the "start" button callback:
while PermitOkay()
pause(0.02);
if MPS_Is_Faulted()
Respond_To_MPS()
end
end
The function MPS_Is_Faulted() looks at a lot of different statuses, does some calculations, and returns a 1 or a 0. No pauses are involved.
The function Respond_To_MPS() looks kind of like this:
function Respond_To_MPS()
if MPS_Unlatch_Okay()
Unlatch() -> another function that uses pause()
end
end
So basically, assuming all of these functions return a 1, I would naively expect functions to be called in the following order:
MPS_Is_Faulted() -> Respond_To_MPS() -> MPS_Unlatch_Okay() -> Unlatch()
However, when I run this program, it seems like MPS_Is_Faulted() gets called many times in a row, then Respond_To_MPS() gets called once.
Is it possible for you to modify those functions for debugging purpose? I would add global variables or persistent variables in those functions to verify whether they are called as expected, or some are called more often than others, just to prove your suspicion. Second, you could comment out all the pause() statements to see if it makes a difference. Lastly, the timer and its callback function sounds very suspicious too. I don't know if you could temporarily disable it to isolate its involvement.
Yes, I can modify them. I already added output from each function to verify my suspicions. Unfortunately, commenting out all of the pause functions isn't really an option, but I can probably comment out some of them. I can probably disable the timer temporarily.
In the meantime, do you know of any functions that could substitute for pause? I do need that functionality in this program for it to work properly.
pause() just pauses the execution of the program. There is really no equivalent function for it. pause() can be used to replace drawnow() for updating GUI purpose because it implicitly called drawnow(). But it should not impact the programming flow. I suspect it is the timer object because pause() certainly interacts with the time.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Tags

Asked:

Ben
on 5 Oct 2011

Community Treasure Hunt

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

Start Hunting!