Table of Contents

Class Engine

Namespace
Loehnert.Lisrt.Scripting.Python
Assembly
Loehnert.Lisrt.Scripting.dll

Class for a script engine.

public class Engine : PropertyChangedBase, INotifyPropertyChangedEx, INotifyPropertyChanged
Inheritance
PropertyChangedBase
Engine
Implements
INotifyPropertyChangedEx
Inherited Members
PropertyChangedBase.Refresh()
PropertyChangedBase.IsNotifying
PropertyChangedBase.PropertyChanged

Constructors

Engine(ILisrtModule, ILockService)

Initializes a new instance of the Engine class.

public Engine(ILisrtModule module = null, ILockService lockService = null)

Parameters

module ILisrtModule

Module with the Engine.

lockService ILockService

Lock service locks the project files while script is running.

Properties

CancellationTokenSource

Gets the token source for canceling the engine.

public CancellationTokenSource CancellationTokenSource { get; }

Property Value

CancellationTokenSource

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)

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.

DebugIsEnabled

Gets or sets a value indicating whether the engine is in debug mode.

public bool DebugIsEnabled { get; set; }

Property Value

bool

Remarks

Debug mode enables breakpoints (BreakPoints), trace (CurrentFrame, 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.

public bool IsCancelableInNonDebug { get; set; }

Property Value

bool

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.

public static bool IsCompatibleToVersion1xx { get; set; }

Property Value

bool

IsInBreak

Gets a value indicating whether the engine is in a break.

public bool IsInBreak { get; }

Property Value

bool

IsRunning

Gets a value indicating whether the script is running.

public bool IsRunning { get; }

Property Value

bool

LastExecutionException

Gets the exception of the last script execution.

public Exception LastExecutionException { get; }

Property Value

Exception

Project

Gets or sets the script project.

public IProject Project { get; set; }

Property Value

IProject

Exceptions

InvalidOperationException

Thrown when IsRunning is true.

Scope

Gets the script scope. This contains the script variables.

public ScriptScopeWrapper Scope { get; }

Property Value

ScriptScopeWrapper

SingleStepExecutionEnabled

Gets or sets a value indicating whether the engine is in single step mode.

public bool SingleStepExecutionEnabled { get; set; }

Property Value

bool

Remarks

Single step means executing line by line. Will be reset to false on breaking.

Exceptions

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.

public bool SingleStepOverExecutionEnabled { get; set; }

Property Value

bool

Remarks

Executes the next line and doesn't steps into it. Will be reset to false on breaking.

Exceptions

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.

public void ClearCachedModules()

Remarks

Imported modules are cached, those should be cleaned if an imported module has been changed since the last execution.

Exceptions

InvalidOperationException

Thrown when an variable named 'modules' is used.

Continue()

Continues the running script when IsInBreak.

public void Continue()

ExecuteFile(string)

Executes a script.

public void ExecuteFile(string path)

Parameters

path string

Relative path to the script file.

Exceptions

ArgumentNullException

Thrown when path is null.

See Also

ExecuteSource(ScriptSource)

Executes a script source.

public void ExecuteSource(ScriptSource source)

Parameters

source ScriptSource

Script source to execute.

Exceptions

ArgumentNullException

Thrown when source is null.

InvalidOperationException

Thrown when the script is already running.

See Also

ExecuteString(string)

Executes a script.

public void ExecuteString(string text)

Parameters

text string

Relative path to the script file.

Exceptions

ArgumentNullException

Thrown when text is null.

RequestBreak()

Requests a break.

public void RequestBreak()

Exceptions

InvalidOperationException

Thrown when IsRunning or DebugIsEnabled is false.

RequestStop()

Requests a script stop.

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.

public event EventHandler<StringWrittenEventArgs> ErrorWritten

Event Type

EventHandler<StringWrittenEventArgs>

OutputWritten

Occurs when an output is written.

public event EventHandler<StringWrittenEventArgs> OutputWritten

Event Type

EventHandler<StringWrittenEventArgs>