Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ShellExecute.cs
1using System.Diagnostics;
2using System;
3using System.Threading.Tasks;
8
10{
15 {
25 public ShellExecute(ScriptNode FileName, ScriptNode Arguments, ScriptNode WorkFolder,
27 : base(new ScriptNode[] { FileName, Arguments, WorkFolder }, argumentTypes3Scalar, Start, Length, Expression)
28 {
29 }
30
34 public override string FunctionName => nameof(ShellExecute);
35
39 public override string[] DefaultArgumentNames => new string[] { "FileName", "Arguments", "WorkFolder" };
40
45 public override bool IsAsynchronous => true;
46
54 {
55 return this.EvaluateAsync(Arguments, Variables).Result;
56 }
57
64 public override Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
65 {
66 if (!(Arguments[0].AssociatedObjectValue is string FileName) ||
67 !(Arguments[1].AssociatedObjectValue is string Arg) ||
68 !(Arguments[2].AssociatedObjectValue is string WorkFolder))
69 {
70 throw new ScriptRuntimeException("Expected string arguments.", this);
71 }
72
73 ProcessStartInfo ProcessInformation = new ProcessStartInfo()
74 {
75 FileName = FileName,
76 Arguments = Arg,
77 UseShellExecute = false,
78 RedirectStandardError = true,
79 RedirectStandardOutput = true,
80 WorkingDirectory = WorkFolder,
81 CreateNoWindow = true,
82 WindowStyle = ProcessWindowStyle.Hidden,
83 ErrorDialog = false
84 };
85
86 Process P = new Process();
87 TaskCompletionSource<IElement> ResultSource = new TaskCompletionSource<IElement>();
88
89 P.ErrorDataReceived += (Sender, e) =>
90 {
91 ResultSource.TrySetException(new ScriptRuntimeException(e.Data, this));
92 };
93
94 P.Exited += async (Sender, e) =>
95 {
96 try
97 {
98 if (P.ExitCode != 0)
99 {
100 string ErrorText = await P.StandardError.ReadToEndAsync();
101 ResultSource.TrySetException(new ScriptRuntimeException(ErrorText, this));
102 }
103 else
104 {
105 string s = await P.StandardOutput.ReadToEndAsync();
106 ResultSource.TrySetResult(new StringValue(s));
107 }
108 }
109 catch (Exception ex)
110 {
111 ResultSource.TrySetException(ex);
112 }
113 };
114
115 Task _ = Task.Delay(1000 * 60 * 5).ContinueWith(Prev => ResultSource.TrySetException(new TimeoutException("Process did not exit within the provided time.")));
116
117 P.StartInfo = ProcessInformation;
118 P.EnableRaisingEvents = true;
119 P.Start();
120
121 return ResultSource.Task;
122 }
123
124 }
125}
Class managing a script expression.
Definition: Expression.cs:39
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes3Scalar
Three scalar parameters.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
ShellExecute(FileName,Arguments,WorkFolder)
Definition: ShellExecute.cs:15
ShellExecute(ScriptNode FileName, ScriptNode Arguments, ScriptNode WorkFolder, int Start, int Length, Expression Expression)
ShellExecute(FileName,Arguments,WorkFolder)
Definition: ShellExecute.cs:25
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: ShellExecute.cs:53
override Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: ShellExecute.cs:64
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: ShellExecute.cs:45
override string FunctionName
Name of the function
Definition: ShellExecute.cs:34
override string[] DefaultArgumentNames
Default Argument names
Definition: ShellExecute.cs:39
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20