Could this be of any use ?
Code:
int sleep_glue(lua_State *L)
{
// We need 1 parameter, no more, no less
if(lua_gettop(L) != 1)
{
lua_pushstring(L, "sleep: parameter mismatch");
lua_error(L);
}
// Execute the sleep
DWORD Duration = (DWORD) lua_tonumber(L, 1)
Sleep(Duration);
// No results
return 0;
} It's a hack, by using the internal sleep function in windows...
It will sleep for the given value in seconds..
A better way, is to constantly check your script, to see if anything has happened..
Code:
int sleep_glue(lua_State *L)
{
// We need 1 parameter, no more, no less
if(lua_gettop(L) != 1)
{
lua_pushstring(L, "sleep: parameter mismatch");
lua_error(L);
}
// Execute the sleep (per 100ms)
DWORD Duration = (DWORD) lua_tonumber(L, 1);
while(Duration > 100)
{
// Abort if necessary...
if(bAbortScript)
return 0;
// Sleep 100ms
Sleep(100);
Duration = Duration - 100;
}
// Execute the remaing part
// Don't execute it if the script is being aborted...
if(!bAbortScript)
Sleep(Duration);
// No results
return 0;
} Where we check the script for action every 100 milliseconds..
But it is a bad hack, since it will consume CPU time like fire will consume gassoline..
You might wanna use windows buildin
WaitForSingleObject like this:
Code:
volatile HANDLE hCloseProgram; // an event handle
hCloseProgram = CreateEvent(NULL, TRUE, FALSE, NULL);
int sleep_glue(lua_State *L)
{
// We need 1 parameter, no more, no less
if(lua_gettop(L) != 1)
{
lua_pushstring(L, "sleep: parameter mismatch");
lua_error(L);
}
// Wait for X milliseconds
// Or return immediatly if hCloseProgram is signaled
DWORD Duration = (DWORD) lua_tonumber(L, 1);
WaitForSingleObject(hCloseProgram, Duration);
// No results
return 0;
} When you want to cancel the WaitForSingleObject, you'd use:
Code:
SetEvent(hCloseProgram);
Of course, event objects still don't abort the LUA script executioner. You also need to modify your hook function to teach it how to use the event object :
Code:
void HookRoutine(lua_State *L, lua_Debug *ar)
{
if(ar->event == LUA_HOOKLINE)
{
if(WaitForSingleObject(hCloseProgram, 0) == WAIT_OBJECT_0)
{
// Ok, let's quit the program
lua_pushstring(L, "HookRoutine: Abort requested!");
lua_error(L);
}
}
} But the be honest, that's not a method I would recommend, the hook routine will be called a lot and calling every time the WaitForSingleObject function causes it to slow down and thus slowing down your scripts! A better method would be a combination of both a global variable to allow aborting of the script engine and an event to allow aborting of functions that suspend the system.
you can for example set only the LUA hook routine when you need to close your program. ie:
Code:
oid AbortRoutine(lua_State *L, lua_Debug *ar)
{
if(ar->event == LUA_HOOKLINE)
{
// Ok, let's abort the script
lua_pushstring(L, "AbortRoutine has been called!");
lua_error(L);
}
}
void OnQuit()
{
// Set a hook for LUA_MASKLINE
lua_sethook(L, AbortRoutine, LUA_MASKLINE, 0);
// Tell the wait functions that we're quiting
SetEvent(hCloseProgram);
} hmm.. guess I can add Lua to my known languages aswell....
//edit
I would have added an example more, but just found out I've once more become an uncle, so I'll guess I'll have a beer instead and celebrate..