static void Main(string[] args) { LuaTask.LuaEnv l = new LuaTask.LuaEnv(); l.ErrorEvent += (e, d) => { Console.WriteLine(d); }; l.DoString(@" sys.timerStart(print,2000,'2 seconds') sys.timerLoopStart(print,1000,'loop') sys.taskInit(function() while true do print('task1',os.time()) sys.wait(1000) end end) sys.taskInit(function() while true do print('task2',os.time()) sys.wait(1000) end end) "); Console.ReadLine(); }
/// <summary> /// 添加一个触发事件 /// 如果虚拟机不存在,则自动新建 /// </summary> /// <param name="name">虚拟机名称</param> /// <param name="type">触发类型名</param> /// <param name="data">回调数据</param> public static void Run(string name, string type, object data) { Log.Info("NewLuaEnv", name); //检查文件是否存在 if (!File.Exists(Utils.Path + "lua/main.lua")) { Log.Error("Lua", $"报错错虚拟机名称:{name}。没有找到入口脚本文件。文件路径应在{Utils.Path}lua/main.lua"); return; } lock (stateLock) { if (!states.ContainsKey(name))//没有的话就初始化池子 { states[name] = new LuaTask.LuaEnv(); states[name].ErrorEvent += (e, text) => { Log.Warn( "Lua插件报错", $"虚拟机运行时错误。名称:{name},错误信息:{text}" ); }; try { states[name].lua.LoadCLRPackage(); states[name].lua["LuaEnvName"] = name; states[name].lua.DoFile(Utils.Path + "lua/main.lua"); } catch (Exception e) { states[name].Dispose(); states.TryRemove(name, out _); Log.Warn( "Lua插件报错", $"虚拟机启动时错误。名称:{name},错误信息:{e.Message}" ); return; } } Log.Debug("lua插件", $"触发事件{type}"); states[name].addTigger(type, data);//运行 } }
static void Main(string[] args) { LuaTask.LuaEnv l = new LuaTask.LuaEnv(); l.ErrorEvent += (e, d) => { Console.WriteLine(d); }; l.DoString(@" import('System') t = TimeSpan(10000000) sys.async('mscorlib','System.Threading.Thread.Sleep',t, function(r,data) print('cb function',r,data,os.time()) end) print(os.time()) "); Console.ReadLine(); }