|
ScriptsMatter Getting Started HOWTO. - Asynchronous mode and cancelation. |
|
|
|
|
Page 3 of 3
Asynchronous mode and cancelation.
- Do the same you did in previous section except of the code for Module1.vb
- Put this code instead of previous one:
Imports System
Imports System.Reflection
Namespace GettingStarted
Module Module1
Friend activeScript As Boolean
Sub Main()
Dim ScriptBody As String = _
"Imports System" & Environment.NewLine & _
"Public Class ScriptingMainClass" & Environment.NewLine & _
"Public Shared Function Main(ByVal Host As SSB.Examples.GettingStarted.MyScriptingHost) As Object" & Environment.NewLine & _
" Host.Log(""Started the cycle for 10 seconds..."")" & Environment.NewLine & _
" Dim CycleStartTime as DateTime = DateTime.UtcNow" & Environment.NewLine & _
" While (DateTime.UtcNow - CycleStartTime).TotalSeconds < 10" & Environment.NewLine & _
" Host.Log(String.Format(""{0} left since the cycle begun."", DateTime.UtcNow - CycleStartTime))" & Environment.NewLine & _
" System.Threading.Thread.Sleep(1000)" & Environment.NewLine & _
" End While" & Environment.NewLine & _
" Return 5+5" & Environment.NewLine & _
"End Function" & Environment.NewLine & _
"End Class" & Environment.NewLine
Try
Dim asyncResult As IAsyncResult = SSB.Runtime.Scripting.Engine(Of Object, MyScriptingHost).BeginCompileAndRun( _
New MyScriptingHost(), "VisualBasic", ScriptBody, _
New String() {"system.dll", "mscorlib.dll", Assembly.GetExecutingAssembly.Location}, New AsyncCallback(AddressOf ScriptCompleted), Nothing)
Console.WriteLine("Press Enter to abort the script before it completes...")
activeScript = True
While activeScript
If Console.KeyAvailable() Then
If Console.ReadKey.Key = ConsoleKey.Enter Then
activeScript = False
Console.WriteLine("Aborting script execution...")
SSB.Runtime.Scripting.Engine(Of Object, MyScriptingHost).AbortCompileAndRun(asyncResult)
End If
End If
End While
Catch ex As Exception
Console.WriteLine("Exception during script execution: {0}", ex)
End Try
Console.WriteLine("Press Enter to exit...")
Console.ReadLine()
End Sub
Sub ScriptCompleted(ByVal ar As IAsyncResult)
activeScript = False
Try
Dim Result As Object = SSB.Runtime.Scripting.Engine(Of Object, MyScriptingHost).EndCompileAndRun(ar)
Console.WriteLine("Script completed and returned result {0}", Result)
Catch ex As Exception
Console.WriteLine("Exception during script execution: {0}", ex)
End Try
End Sub
End Module
Public Class MyScriptingHost
Inherits MarshalByRefObject
Public Sub Log(ByVal message As String)
Console.WriteLine(message)
End Sub
End Class
End Namespace
Script will start work asynchronously. You may abort it pressing Enter button during 10 seconds after the script started to work or you may wait until it completes and return the result.
Congratulations! You are able to embed script support in your application and execute them in asynchronous mode as well as to abort its execution.
That’s it for the beginning. There are other points of cause, but I am pretty sure you are smart and will be able to overcome them.
In case you have any question, please do not hesitate to contact us:
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
|