diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2021-06-06 11:29:05 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2021-06-06 11:29:05 +0300 |
| commit | 50c254dfe0b7063476ee9d7aca9e48ca4eaa9dd1 (patch) | |
| tree | 39bb1fc0ee710b04f5e2766815a06f005eddda20 | |
| parent | 7b96c9b0e6d322e9f0ea853c15232bb9b9a1ff75 (diff) | |
| download | Tango-50c254dfe0b7063476ee9d7aca9e48ca4eaa9dd1.tar.gz Tango-50c254dfe0b7063476ee9d7aca9e48ca4eaa9dd1.zip | |
Added OnExit support for FSE procedures.
3 files changed, 44 insertions, 1 deletions
diff --git a/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/IProcedureContext.cs b/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/IProcedureContext.cs index ff45a7df4..da74bb420 100644 --- a/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/IProcedureContext.cs +++ b/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/IProcedureContext.cs @@ -715,5 +715,11 @@ namespace Tango.FSE.Procedures /// <param name="symbolsMap">The symbols map.</param> [HideIntellisense] void BreakPoint(String file, int lineNumber, params Object[] symbolsMap); + + /// <summary> + /// Gets or sets the optional exit action for this script. + /// </summary> + [HideIntellisense] + Action<IProcedureContext> OnExitAction { get; set; } } } diff --git a/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/ProcedureContext.cs b/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/ProcedureContext.cs index f8226f6c5..1fea95cf3 100644 --- a/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/ProcedureContext.cs +++ b/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/ProcedureContext.cs @@ -78,6 +78,8 @@ namespace Tango.FSE.Procedures get { return MachineProvider.Machine; } } + public Action<IProcedureContext> OnExitAction { get; set; } + public ProcedureContext(ProcedureProject project, IProcedureLogger logger) { _project = project; 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<T> session = null; + var prop = typeof(T).GetProperty("OnExitAction"); + bool exitActionExecuted = false; + session = new ProjectSession<T>(this, () => { + Action<T> onExitAction = prop?.GetValue(context) as Action<T>; + + 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(); }); |
