@@ -108,6 +108,90 @@ public static ExecutionResult ExecWithResult(this Interpreter interpreter,bool c
108108 return result ;
109109 }
110110
111+ /// <summary>
112+ /// Execute the program and return the results Asynchronous
113+ /// </summary>
114+ /// <param name="interpreter">The interpreter</param>
115+ /// <param name="copyOfTheVariables">Optional,if true return a copy of the variables otherwise a reference to them</param>
116+ /// <returns>the results</returns>
117+ public async static Task < ExecutionResult > ExecWithResultAsync ( this Interpreter interpreter , bool copyOfTheVariables = false )
118+ {
119+ bool exception = false ;
120+ ExecutionResult result = new ( ) ;
121+ try
122+ {
123+ result . programStartWhen = DateTime . Now ;
124+ await interpreter . ExecAsync ( ) ;
125+ result . programEndWhen = DateTime . Now ;
126+ result . hasError = false ;
127+ result . value = interpreter . Result ;
128+ if ( copyOfTheVariables ) // (v) copy of the variables
129+ {
130+ var variables = interpreter . GetVariables ( ) ;
131+ result . variables = new ( ) ;
132+ foreach ( var item in variables )
133+ {
134+ result . variables . Add ( item . Key , new Value ( item . Value ) ) ;
135+ }
136+ }
137+ else
138+ {
139+ result . variables = interpreter . GetVariables ( ) ; // Reference to the variables
140+ }
141+ }
142+ catch ( FBasicException e )
143+ {
144+ result . hasError = true ;
145+ result . errorText = e . ToString ( ) ;
146+ result . lineOfError = e . line ;
147+ result . errorSourceLine = e . sourceLine ;
148+ #if DEBUG
149+ result . exception = e ;
150+ #endif
151+ exception = true ;
152+ }
153+ catch ( IndexOutOfRangeException e1 )
154+ {
155+ result . hasError = true ;
156+ result . errorText = Errors . E130_OutOfRange ( interpreter . lex . Identifier ) ;
157+ result . lineOfError = interpreter . lex . CurrentSourceMarker . Line ;
158+ result . errorSourceLine = interpreter . lex . GetLine ( result . lineOfError ) ;
159+ #if DEBUG
160+ result . exception = e1 ;
161+ #endif
162+ exception = true ;
163+ }
164+ catch ( Exception ex )
165+ {
166+ result . hasError = true ;
167+ result . lineOfError = interpreter . lex . CurrentSourceMarker . Line ;
168+ result . errorText = Errors . X012_GeneralException ( "" , ex , $ " Near line { result . lineOfError } ") ;
169+ result . errorSourceLine = interpreter . lex . GetLine ( result . lineOfError ) ;
170+ #if DEBUG
171+ result . exception = ex ;
172+ #endif
173+ exception = true ;
174+ }
175+ finally
176+ {
177+ if ( exception ) result . programEndWhen = DateTime . Now ;
178+ foreach ( var lib in interpreter . librariesWithMemory )
179+ {
180+ if ( lib . Value is IDisposable )
181+ {
182+ try // do not produce errors from Dispose
183+ {
184+ ( ( IDisposable ) lib . Value ) . Dispose ( ) ;
185+ }
186+ catch
187+ {
188+ }
189+ }
190+ }
191+ }
192+ return result ;
193+ }
194+
111195
112196 /// <summary>
113197 /// Get a value of any name (variable, collection item)
0 commit comments