private static void runTigger() { try { while (true) { System.Threading.Thread.Sleep(1); if (tokenSource.IsCancellationRequested) { return; } while (toRun.Count > 0) { try { LuaPool temp; toRun.TryTake(out temp); lua.Global.Get <XLua.LuaFunction>("tiggerCB").Call(temp.id, temp.type, temp.data); } catch (Exception le) { LuaApis.PrintLog("回调报错:\r\n" + le.ToString()); } if (tokenSource.IsCancellationRequested) { return; } } } } catch (Exception ex) { StopLua(ex.ToString()); } }
private static void runTigger() { try { while (true) { Task.Delay(1).Wait(); if (tokenSource.IsCancellationRequested) { return; } if (toRun.Count > 0) { try { var temp = toRun[0]; toRun.RemoveAt(0); lua.Global.Get <XLua.LuaFunction>("tiggerCB").Call(temp.id, temp.type, temp.data); } catch (Exception le) { LuaApis.PrintLog("回调报错:\r\n" + le.ToString()); } if (tokenSource.IsCancellationRequested) { return; } } } } catch (Exception ex) { StopLua(ex.ToString()); } }
private static void runTigger() { try { while (true) { Task.Delay(1).Wait(); if (tokenSource.IsCancellationRequested) { return; } if (toRun.Count > 0) { try { lua.exec("tiggerCB", toRun[0].id, toRun[0].type, toRun[0].data); } catch (Exception le) { LuaApis.PrintLog("回调报错:\r\n" + le.ToString()); } if (tokenSource.IsCancellationRequested) { return; } toRun.RemoveAt(0); } } } catch (Exception ex) { StopLua(ex.ToString()); } }
/// <summary> /// 初始化lua对象 /// </summary> /// <param name="lua"></param> public static void Initial(XLua.LuaEnv lua, string t = "script") { //utf8转gbk编码的hex值 lua.DoString("apiUtf8ToHex = CS.llcom.LuaEnv.LuaApis.Utf8ToAsciiHex"); //获取软件目录路径 lua.DoString("apiGetPath = CS.llcom.LuaEnv.LuaApis.GetPath"); //输出日志 lua.DoString("apiPrintLog = CS.llcom.LuaEnv.LuaApis.PrintLog"); //获取快捷发送区数据 lua.DoString("apiQuickSendList = CS.llcom.LuaEnv.LuaApis.QuickSendList"); //输入框 lua.DoString("apiInputBox = CS.llcom.LuaEnv.LuaApis.InputBox"); if (t != "send") { //发送串口数据 lua.DoString("apiSendUartData = CS.llcom.LuaEnv.LuaApis.SendUartData"); //定时器 lua.DoString("apiStartTimer = CS.llcom.LuaEnv.LuaRunEnv.StartTimer"); lua.DoString("apiStopTimer = CS.llcom.LuaEnv.LuaRunEnv.StopTimer"); } //加上需要require的路径 lua.DoString(@" local rootPath = '" + LuaApis.Utf8ToAsciiHex(LuaApis.GetPath()) + @"' rootPath = rootPath:gsub('[%s%p]', ''):upper() rootPath = rootPath:gsub('%x%x', function(c) return string.char(tonumber(c, 16)) end) package.path = package.path.. ';'..rootPath..'core_script/?.lua'.. ';'..rootPath..'?.lua'.. ';'..rootPath..'user_script_run/requires/?.lua' package.cpath = package.cpath.. ';'..rootPath..'core_script/?.lua'.. ';'..rootPath..'?.lua'.. ';'..rootPath..'user_script_run/requires/?.lua' "); //运行初始化文件 lua.DoString("require 'core_script.head'"); }
/// <summary> /// 停止运行lua /// </summary> public static void StopLua(string ex) { LuaRunError(null, EventArgs.Empty); if (ex != "") { LuaApis.PrintLog("lua代码报错了:\r\n" + ex); } else { LuaApis.PrintLog("lua代码已停止"); } foreach (var v in pool) { v.Value.Cancel(); } isRunning = false; tokenSource.Cancel(); pool.Clear(); lua = null; }