From: John L. <jla...@gm...> - 2009-05-20 00:48:26
|
On Tue, May 19, 2009 at 2:14 PM, Youen Toupin <you...@wa...>wrote: > John Labenski a écrit : > > I think that all you need to do is to shuffle around your existing > > code to pull out the dialog creation from the coroutine code, I also > > demonstrate how to pass data around without using globals. > Yes, this is a solution, but I would like to avoid it if possible. My > idea is to use coroutines to have a flexible system that would allow > sequential programming to perform tasks even when these tasks require > user action, or waiting for anything without blocking the application. > The promptUser function is only an example. I'd like to be able to write > things like: > I'm not sure I understand. GUI programs usually don't work this way, they are event loop based and therefore they are not sequential. If you want to pop up a dialog that is non-modal simply use wxDialog:Show(true) instead of ShowModal(). In any case, if you really want to use coroutines you can use the below. I think your problem was simply that you were calling coroutine.resume from within the coroutine so all the code does below is make sure that the callback() function which will call coroutine.resume() is called from the main Lua state. I would probably go with the c_fn() way, which would allow you to use far more local vars, but you'll have to make quite a few more mods to pass more data around as params before you can get rid of all the globals... Regards, John ============================== -- I overwrite print and assert functions to get messages in the console window --local print = print -- don't want wxLua print function local assert = function( cond, msg, ... ) if not cond then print( "Assertion failed: "..msg ) print( debug.traceback() ) end return cond, msg, ... end require( "wx" ) function callback() print( "Button clicked" ) assert( coroutine.resume( co ) ) end function connect_fn() dialog:Connect( button:GetId(), wx.wxEVT_COMMAND_BUTTON_CLICKED, callback ) end function createDialog() dialog = wx.wxDialog( wx.NULL, wx.wxID_ANY, "Test dialog", wx.wxDefaultPosition, wx.wxDefaultSize ) panel = wx.wxPanel( dialog, wx.wxID_ANY ) sizer = wx.wxBoxSizer( wx.wxHORIZONTAL ) button = wx.wxButton( panel, wx.wxID_ANY, "Test button" ) sizer:Add( button ) panel:SetSizer( sizer ) sizer:SetSizeHints( dialog ) dialog:Show( true ) end co = coroutine.create( function() createDialog() print( "Yield..." ) coroutine.yield(connect_fn) print( "After yield" ) coroutine.yield() print( "After yield 2" ) coroutine.yield() print( "After yield 3" ) end ) ret, c_fn = coroutine.resume( co ) print(ret, c_fn, dialog, button) -- each of these ways works --c_fn() --dialog:Connect( button:GetId(), wx.wxEVT_COMMAND_BUTTON_CLICKED, callback ) connect_fn() wx.wxGetApp():MainLoop() |