Hello Guest it is April 19, 2024, 04:31:40 PM

Author Topic: LUA problem when exit a Wizard  (Read 1352 times)

0 Members and 1 Guest are viewing this topic.

LUA problem when exit a Wizard
« on: July 16, 2018, 03:01:17 AM »
Hi,
I have written a couple of wizards for my needs in Mach4, been using wxFormBuilder for generating the GUI. So far so good. My problem is that when I have two wizards running at the same time, I don’t succeed to exit the second one. As long as only one is running there is no problem at all. Been looking through the code for the wizards that comes with Mach4 and I can't for my life not understand what the difference in the exit code in these are compared with mine. You can without any problem have several instances of them and exit all without problem…     


This is my “exit” function:
Code: [Select]
function ExitWizard()

UI.MainWindow:Destroy()

if(LogWindowOpen or UI.Log1 ) then
UI.Log1:Destroy()
end

end

When exit the first wizard everything is working fine, but when I try to exit the second one I get this error message in Mach4 and nothing happens..

Code: [Select]
Lua: Error while running chunk
C:\Mach4Hobby\Wizards\BossProbe.mcs:131: attempt to index field 'MainWindow' (a userdata value)
stack traceback:
C:\Mach4Hobby\Wizards\BossProbe.mcs:131: in function 'ExitWizard'
C:\Mach4Hobby\Wizards\BossProbe.mcs:464: in function <C:\Mach4Hobby\Wizards\BossProbe.mcs:461>

Would be grateful for any advice on this one, obviously it’s working with the supplied wizards.

Regards,
Kjell

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: LUA problem when exit a Wizard
« Reply #1 on: July 16, 2018, 03:24:29 AM »
I have no idea if the wizard launcher opens the wizards in a private enviroment or is launching from the main stack, if you have created with formbuilder and both wizards have UI.MainWindow it could be the fact that when you destroy one the table UI has been destroyed or UI.Mainframe , in formbuilder try changing the table prefix so they are unique.

DazTheGas
New For 2022 - Instagram: dazthegas
Re: LUA problem when exit a Wizard
« Reply #2 on: July 16, 2018, 09:01:49 AM »
Right, I was thinking around the same lines. But that doesn’t help if you start two of the same wizard (which has happened to me a number of times

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: LUA problem when exit a Wizard
« Reply #3 on: July 16, 2018, 01:56:44 PM »
it is the UI.MainWindow:Destroy() that is causing the problem, heres a more simpler aproach using the wx.wxEVT_CLOSE_WINDOW event
Code: [Select]
----------------------------------------------------------------------------
-- Lua code generated with wxFormBuilder DTG Version(Jan 2018)
----------------------------------------------------------------------------

UI = {}

-- create MainWindow
UI.MainWindow = wx.wxFrame (wx.NULL, wx.wxID_ANY, "", wx.wxDefaultPosition, wx.wxSize( 500,300 ), wx.wxDEFAULT_FRAME_STYLE+wx.wxTAB_TRAVERSAL )
UI.MainWindow:SetSizeHints( wx.wxDefaultSize, wx.wxDefaultSize )

UI.bSizer1 = wx.wxBoxSizer( wx.wxVERTICAL )

UI.m_button1 = wx.wxButton( UI.MainWindow, wx.wxID_ANY, "MyButton", wx.wxDefaultPosition, wx.wxDefaultSize, 0 )
UI.bSizer1:Add( UI.m_button1, 0, wx.wxALL, 5 )

UI.m_button2 = wx.wxButton( UI.MainWindow, wx.wxID_ANY, "MyButton", wx.wxDefaultPosition, wx.wxDefaultSize, 0 )
UI.bSizer1:Add( UI.m_button2, 0, wx.wxALL, 5 )


UI.MainWindow:SetSizer( UI.bSizer1 )
UI.MainWindow:Layout()

UI.MainWindow:Centre( wx.wxBOTH )
UI.MainWindow:Show()

-- Connect Events

UI.MainWindow:Connect( wx.wxEVT_CLOSE_WINDOW, function(event)
--do exit commands

event:Skip()
end )

There are a couple of ways you can make sure you have only one window
1. add to the MainWindow creation wx.wxSTAY_ON_TOP so its visible at all times so you know one is already open.
2. add a register so before the wizard is created it checks this register to see if its active IE if MainWindow is active then register == 1 and on exit the register == 0

DazTheGas
New For 2022 - Instagram: dazthegas
Re: LUA problem when exit a Wizard
« Reply #4 on: July 17, 2018, 01:21:02 AM »
Thanks for pointing me in the right direction. I was doing some more testing and found that the only way I could get it to work was a “clean”:
Code: [Select]
UI.MainWindow:Connect( wx.wxEVT_CLOSE_WINDOW, function(event)
--implements MainFrameClose



event:Skip()
end )


I have an “Exit” button in the wizard and I changed so that one call Close() instead of Destroy():

Code: [Select]
function ExitWizard()

UI.MainWindow:Close()

end

After some reading, my understanding is that Close() should generate a wxEVT_CLOSE_WINDOW event, which should trigger the close of the window. Problem is that when you have more than one instance of the wizard running, which have the same code, it will not work. It works on the first window you try to close but then on the second the LUA runtime addresses the same “UI.MainWindow” as in the first one and you get the error say trying to address something that doesn’t exist anymore.
So right now it seems to be impossible to have two or more wizards that have the same name on the wxFrame object to run at the same time. This is of course not a big problem, normally you don’t run two of the same wizards at the same time. But at least I can close them now if starting more by mistake