LuaCOM and message loops

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

LuaCOM and message loops

Paul Hudson-2

I’m using Lua50 (LuaBinaries distribution) and LuaCOM.  I am not a COM expert L

 

I would like to know how to handle events that are being raised asynchronously. I think I need a message loop of some sort, but I’m not clear (after reading the documentation) on how to do this.

 

I have a COM component with a method that begins execution of a command on a remote device (the details aren’t important, I think) and then returns

 

While the command is executing and producing output, events are raised. But because my Lua script is continuing to execute without processing messages in some message loop, the script never receives events.

 

The example from the LuaCOM documentation using the AfterUpdate event of MSCAL.Calendar works fine – but that event is raised during the call from Lua of calendar.NextMonth, so doesn’t have the same asynchronous problem (the component I’m using has similar events, and they’re working fine too).

 

I have a VB script that does the equivalent to the Lua script and that’s working fine too (presumably because the Windows Scripting Host is doing something with message loops)

 

I’m missing something, probably something obvious.

 

Any pointers?

 

P.

 

Reply | Threaded
Open this post in threaded view
|

Re: LuaCOM and message loops

Fabio Mascarenhas-2
Hi,

You can use luacom.StartMessageLoop to start a Windows message loop. To
this function, you can pass a function and a list of arguments, and this
function will be called in each iteration of the loop:

luacom.StartMessageLoop(mycode, arg1, arg2)

You can look at how this function is implemented to implement the same
thing with coroutines (so you would call yield whtn you want another
iteration of the message loop, instead of returning). LuaCOM had to work
with Lua 4.0, also, so it does not use coroutines.

--
Fabio Mascarenhas

On Wed, Apr 26, 2006 at 04:15:41PM +0100, Paul Hudson wrote:
>    I'm using Lua50 (LuaBinaries distribution) and LuaCOM.  I am not a COM
>    expert L
>    I would like to know how to handle events that are being raised
>    asynchronously. I think I need a message loop of some sort, but I'm not
>    clear (after reading the documentation) on how to do this.
>    P.
>
>

signature.asc (198 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: LuaCOM and message loops

Paul Hudson-2

>You can use luacom.StartMessageLoop to start a Windows message loop.

Thanks, that worked. All my events are successfully handled now.

> To this function, you can pass a function and a list of arguments,
> and this function will be called in each iteration of the loop:

I passed a simple "function () print ("Here") end" to this, and it does get
called - but only once, as far as I can tell? And StartMessageLoop doesn't
seem to return to my script.

>You can look at how this function is implemented to implement the same
>thing with coroutines (so you would call yield whtn you want another
>iteration of the message loop, instead of returning). LuaCOM had to work
>with Lua 4.0, also, so it does not use coroutines.

I'm not familiar enough with the LUA C API to make much progress here; I can
look at the code but I don't know what it does or how to modify it.

P.

Reply | Threaded
Open this post in threaded view
|

Re: LuaCOM and message loops

Fabio Mascarenhas-2
Hi,

On Thu, Apr 27, 2006 at 09:50:39AM +0100, Paul Hudson wrote:
> > To this function, you can pass a function and a list of arguments,
> > and this function will be called in each iteration of the loop:
>
> I passed a simple "function () print ("Here") end" to this, and it does get
> called - but only once, as far as I can tell? And StartMessageLoop doesn't
> seem to return to my script.

Hmm, this is a bug. Thanks for noticing.
 
> >You can look at how this function is implemented to implement the same
> >thing with coroutines (so you would call yield whtn you want another
> >iteration of the message loop, instead of returning). LuaCOM had to work
> >with Lua 4.0, also, so it does not use coroutines.
>
> I'm not familiar enough with the LUA C API to make much progress here; I can
> look at the code but I don't know what it does or how to modify it.

Another approach is to write a function to run a single iterarion of the
message loop, and call it from your script (move the loop to the
script, basically). Pretty easy to write:

int luacom_MessageLoopStep(lua_State *L)
{
  MSG msg;
  if(GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return 0;
}

Now add this to the functions_tb array, in luacom.cpp:

{"MessageLoopStep", luacom_MessageLoopStep}

And rebuild Luacom (there is a Makefile that works with Microsoft's
nmake). This adds a luacom.MessageLoopStep function that runs a single
iterarion of the Windows Message Loop.
 
> P.
>

--
Fabio Mascarenhas


signature.asc (198 bytes) Download Attachment