Page 1 of 1

wx.wxMessageBox with lua question

Posted: Wed Nov 29, 2017 2:26 am
by gorf25
I have a button event that calls a function i have it running a stepper motor, and running though a loop till the motor reaches the distance it was told to travel. The GUI seems to lockup every time in the loop making a call to check the motor position.

But the strange thing is if i put a wx.wxMessageBox in the loop it runs fine the problem is, i have to keep hitting the enter key or the clicking the ok button, i can even leave the wx.wxMessageBox up without clicking it for a little while and the motor and position still keep moving once it reaches the end of loop it continues ok from there after i hit enter to close the wx.wxMessageBox.

Is there a way to simulate this without using a wx.wxMessageBox and having to hit enter or a button press in the loop?

So far anything other way i have tried seems lockup the GUI..

Thanks gary

Re: wx.wxMessageBox with lua question

Posted: Wed Nov 29, 2017 9:59 am
by PB
(I know nothing about lua, I am speaking from the viewpoint of a C++ programmer).

Well, if I understand it correctly, the application behaves as expected, if you enter a loop in the main thread, the message queue will not be processed and GUI becomes unresponsive.

wxMessageBox is probably implemented as a modal dialog which has its own message queue so it "works". But as it is application modal, the main program GUI still cannot be used, right?

The solution depends on the task, i.e., mostly on how long the task takes, if it is expected to be interruptible by the user, and whether the application is supposed to be used for doing other tasks while the motor is being repositioned. Depending on this, either a worker thread or a progress dialog updated within the loop should be used.

I do not think it could be possible to use wxTimer and move the motor in small steps in response to each timer event until the desired position is reached - if the call to move the motor is blocking and it takes more than a fraction of a second, the UI would probably become jumpy, not to mention that the motor might prefer to be moved from the starting to the desired position in a single step.

Re: wx.wxMessageBox with lua question

Posted: Wed Nov 29, 2017 1:46 pm
by gorf25
Yes it behaves as expected as long as I have the wx.messagebox in the loop in the main thread there is a DRO that displays the distance being traveled while the motor is moving and that works also.

If I remove the wx.messagebox from the loop and call the function that reads the distance of current motor position the GUI locks up and the DRO stops showing the motor distance, some times after a minute or so, I assume it reached its travel distance the GUI frees unlocks, and the DRO will just jump from 0 to the entered distance it has traveled in stead of showing the movement on the DRO from 0 to entered distance at the motor speed.

if I add the wx.messagebox to the loop every thing works correct, but I don't want to have user input in the loop..

I don't think the motor will ever move in a single step most of the time depending on the stepper it would take an average of 1800 or so steps to move an inch I don't remember off the top to my head but I think it around 250 steps per 1 motor turn.

What do you think the wx.messagebox is doing that would make the loop and distance check work correctly but without it everything seems to lock up the then frees back up, I assume the loop is completed then, if I click anywhere in the program window I will get the message program not responding endless I have the wx.messagebox in the loop

Hope this makes more sense


Re: wx.wxMessageBox with lua question

Posted: Wed Nov 29, 2017 4:17 pm
by PB
I do not have much to add to what I wrote above.

In case you do not know, there is a bunch of *yield* methods in wxAppConsole and wxApp. You can look into this, I have never used them and TBH think these are kind of last resort hacks...

I still believe that a worker thread which notifies the main thread about the progress (or use a progress dialog and update it from within the loop if the application is to not do anything else anyway) is the best idea but I know nothing about wxLua or the actual communication with the device...

Re: wx.wxMessageBox with lua question

Posted: Wed Nov 29, 2017 10:00 pm
by gorf25
Not sure yet if its working totally correct but I setup an timer event
i click a button that calls the function to move the motor
Then exit the function and turn on the timer that calls the function to check the distance motor has traveled
set timer to 1000 for now

Only tried it a couple times so far but seems to work no wx.messagebox and no user input also the DRO movers up correctly
So the timer event may be the way to go.

Thanks gary