From 50c254dfe0b7063476ee9d7aca9e48ca4eaa9dd1 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Sun, 6 Jun 2021 11:29:05 +0300 Subject: Added OnExit support for FSE procedures. --- .../Scripting/Tango.Scripting.Basic/Project.cs | 37 +++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs') diff --git a/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs b/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs index 7500e404f..30fbed006 100644 --- a/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs +++ b/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs @@ -147,7 +147,14 @@ namespace Tango.Scripting.Basic } else { - code += Environment.NewLine + Environment.NewLine + "new Program().OnExecute(GlobalContext);"; + if (code.Contains("OnExit(IProcedureContext context)")) + { + code += Environment.NewLine + Environment.NewLine + "var pr = new Program(); GlobalContext.OnExitAction = pr.OnExit; pr.OnExecute(GlobalContext);"; + } + else + { + code += Environment.NewLine + Environment.NewLine + "new Program().OnExecute(GlobalContext);"; + } mainScriptCode = code; } } @@ -208,8 +215,36 @@ namespace Tango.Scripting.Basic Thread scriptThread = null; ProjectSession session = null; + var prop = typeof(T).GetProperty("OnExitAction"); + bool exitActionExecuted = false; + session = new ProjectSession(this, () => { + Action onExitAction = prop?.GetValue(context) as Action; + + if (onExitAction != null && !exitActionExecuted) + { + exitActionExecuted = true; + bool completed = false; + DateTime startDate = DateTime.Now; + + Task.Factory.StartNew(() => + { + onExitAction(context); + completed = true; + }); + + while (!completed) + { + Thread.Sleep(10); + + if (DateTime.Now > startDate.AddSeconds(5)) + { + break; + } + } + } + scriptThread.Abort(); }); -- cgit v1.3.1