Class Engine
Class for a script engine.
Implements
Inherited Members
Namespace: Loehnert.Lisrt.Scripting.Python
Assembly: Loehnert.Lisrt.Scripting.dll
Syntax
public class Engine : PropertyChangedBase, INotifyPropertyChangedEx, INotifyPropertyChanged
Constructors
Engine(ILisrtModule, ILockService)
Initializes a new instance of the Engine class.
Declaration
public Engine(ILisrtModule module = null, ILockService lockService = null)
Parameters
Type | Name | Description |
---|---|---|
ILisrtModule | module | Module with the Engine. |
ILockService | lockService | Lock service locks the project files while script is running. |
Properties
CancellationTokenSource
Gets the token source for canceling the engine.
Declaration
public CancellationTokenSource CancellationTokenSource { get; }
Property Value
Type | Description |
---|---|
System.Threading.CancellationTokenSource |
Remarks
You can access this property in the script by the name "CancellationTokenSource". The token source will is a new instance, after each script execution.
Examples
This example shows ho to create a method that can be canceled, and how to use it in a Python script.
public static void Sleep(double timeInSeconds, CancellationToken cancellationToken)
{
var stopWatch = Stopwatch.StartNew();
while (stopWatch.Elapsed.TotalSeconds < timeInSeconds
&& !cancellationToken.IsCancellationRequested)
{
Thread.Sleep(20);
}
}
The Sleep method is canceled, when the user presses the cancel button or the RequestStop() method is executed.
demo.Helpers.Sleep(10, CancellationTokenSource.Token)
DebugIsEnabled
Gets or sets a value indicating whether the engine is in debug mode.
Declaration
public bool DebugIsEnabled { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Remarks
Debug mode enables breakpoints (BreakPoints), trace (Loehnert.Lisrt.Scripting.Python.Engine.CurrentFrame, Loehnert.Lisrt.Scripting.Python.Engine.CallStack), break (RequestBreak()) the single step (SingleStepExecutionEnabled) and the single step over mode (SingleStepOverExecutionEnabled).
IsCancelableInNonDebug
Gets or sets a value indicating whether the script can be canceled by the CancellationTokenSource. Default is true. When the DebugIsEnabled property is true, a running script can always be canceled.
Declaration
public bool IsCancelableInNonDebug { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Remarks
When this property is false and the DebugIsEnabled property is false too, no trace back is set and you should have a faster script.
IsCompatibleToVersion1xx
Gets or sets a value indicating whether all project folders should be added to python search paths. Use this only for compatibility with script projects created below version 2.0.0.
Declaration
public static bool IsCompatibleToVersion1xx { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
IsInBreak
Gets a value indicating whether the engine is in a break.
Declaration
public bool IsInBreak { get; }
Property Value
Type | Description |
---|---|
Boolean |
IsRunning
Gets a value indicating whether the script is running.
Declaration
public bool IsRunning { get; }
Property Value
Type | Description |
---|---|
Boolean |
LastExecutionException
Gets the exception of the last script execution.
Declaration
public Exception LastExecutionException { get; }
Property Value
Type | Description |
---|---|
System.Exception |
Project
Gets or sets the script project.
Declaration
public IProject Project { get; set; }
Property Value
Type | Description |
---|---|
IProject |
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Thrown when IsRunning is true. |
Scope
Gets the script scope. This contains the script variables.
Declaration
public ScriptScopeWrapper Scope { get; }
Property Value
Type | Description |
---|---|
ScriptScopeWrapper |
SingleStepExecutionEnabled
Gets or sets a value indicating whether the engine is in single step mode.
Declaration
public bool SingleStepExecutionEnabled { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Remarks
Single step means executing line by line. Will be reset to false on breaking.
Exceptions
Type | Condition |
---|---|
System.ArgumentException | Thrown when new value is true and SingleStepExecutionEnabled is true. |
SingleStepOverExecutionEnabled
Gets or sets a value indicating whether the engine is in a single step over mode.
Declaration
public bool SingleStepOverExecutionEnabled { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Remarks
Executes the next line and doesn't steps into it. Will be reset to false on breaking.
Exceptions
Type | Condition |
---|---|
System.ArgumentException | Thrown when new value is true and SingleStepExecutionEnabled is true. |
Methods
ClearCachedModules()
Clears the cached modules from the scope. Warning: Calling this method to often can cause a memory leak. Sometimes some cached modules are not completely destroyed.
Declaration
public void ClearCachedModules()
Remarks
Imported modules are cached, those should be cleaned if an imported module has been changed since the last execution.
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Thrown when an variable named 'modules' is used. |
Continue()
Continues the running script when IsInBreak.
Declaration
public void Continue()
ExecuteFile(String)
Executes a script.
Declaration
public void ExecuteFile(string path)
Parameters
Type | Name | Description |
---|---|---|
String | path | Relative path to the script file. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown when |
See Also
ExecuteSource(ScriptSource)
Executes a script source.
Declaration
public void ExecuteSource(ScriptSource source)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.Scripting.Hosting.ScriptSource | source | Script source to execute. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown when |
System.InvalidOperationException | Thrown when the script is already running. |
See Also
ExecuteString(String)
Executes a script.
Declaration
public void ExecuteString(string text)
Parameters
Type | Name | Description |
---|---|---|
String | text | Relative path to the script file. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown when |
RequestBreak()
Requests a break.
Declaration
public void RequestBreak()
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Thrown when IsRunning or DebugIsEnabled is false. |
RequestStop()
Requests a script stop.
Declaration
public void RequestStop()
Remarks
This method requests the script to stop. However, this does not mean that the script will stop immediately, only when the currently executing Python command has finished.
Events
ErrorWritten
Occurs when an error is written.
Declaration
public event EventHandler<StringWrittenEventArgs> ErrorWritten
Event Type
Type | Description |
---|---|
System.EventHandler<StringWrittenEventArgs> |
OutputWritten
Occurs when an output is written.
Declaration
public event EventHandler<StringWrittenEventArgs> OutputWritten
Event Type
Type | Description |
---|---|
System.EventHandler<StringWrittenEventArgs> |