|
Hi
Thanks for the reply. I went with a simple solution of using a mutex, so that when I was executing in the Lua code I take the mutex and then when the script calls sleep and exits back out into the C# sleep function I release the mutex so that event handlers are safe to call into the Lua core again to execute a Lua callback function. I have tested this pretty heavily it seems solid now. I have had 4 fast timers created and running from a Lua script without a crash. As a follow up question, I was maybe hoping in the future I could expand on what I have now to spin up two Windows threads from C# and then execute two entirely separate Lua contexts (both use LuaInterface of course) I was thinking this should be threadsafe as the start of the task does something like this. lua = new Lua(); lua.RegisterFunction("printToApp", this, GetType().GetMethod("printToApp")); This idea seems to be flawed though from a quick experiment I did. Having two threads each create a new Lua instance and register a few simple functions always crashes on execution. Does anyone have some insight into is it possible to run 2 totally independent threads with LuaInterface ? Any help would be appreciated. Thanks Geoff Date: Tue, 18 Nov 2014 12:32:34 +0100 From: marco@mastropaolo.com To: lua-l@lists.lua.org Subject: Re: Tricky c#/LuaInterface Question Sorry for the typos and missing links.. I'm on mobile. There are several solutions : 1. Use another time class. .NET has a ton of timers most of which dispatch on a single thread in a non reentrant way. They are less precise but easier to work with. 2. Set the synchronization object of the timer to some UI element for automatic dispatching. 3. Use a Dispatcher or a SynchronizationContext to dispatch execution on the thread you want. 4. Set AutoReset to false - this should suffice to work (you have to manually restart the timer though). I would consider using a lock to handle concurrency in a safe way, though. Note that as far as I know, Lua and LuaInterface will not complain if you use it from different threads as long as it's only one thread at the time. I might be wrong though. Shameless plug: you can also check other interface layers for C# and Lua like NLua, SharpLua, MoonSharp, UniLua, and KopiLua. As I'm the author of one of these I listed them in random order but all are good ;) None will help you much in this scenario but should be easier to handle on other aspects. Il 18/nov/2014 11:25 "Geoff Smith" <spammealot1@live.co.uk> ha scritto:
|