CS-Script 3.20.0

"Isolated execution" pattern

"Isolated execution" pattern represents the simplest execution scenario. According to this pattern a host application and a script have no knowledge about each other. In fact the script execution is not very different from execution from command prompt the except, that the execution is initiated by the application but not by the user.

Example

Let's assume we have script SendMail.cs, which that contains implementation of sending an e-mail. And it uses command-line arguments to obtain the e-mail address, subject and e-mail text. If it is required to send an email from the command prompt this command would look like this:

cscs SendMail smith@gmail.com Test "This is just a test"

 

Script execution within current process:

Any script can be executed from the host application within a current process. This is because the script engine is available not only as an executable but also as a class library (CSScriptLibrary.dll).

using System;
using CSScriptLibrary;


class SendMail
{
    static void Main(string [] args)
    {
        string[] params = new string[] 
                    {"SendMail""smith@gmail.com""Test""This is just a test"};


        CSScript.Execute(null, params);
    }
}

 

Script execution as a separate process:

Any script can be also executed from the host application as a separate process. In this case the script code for sending the e-mail would be as simple as:

RunScript("SendMail smith@gmail.com Test \"This is just a test\"");

The following C# code is an implementation of the RunScript method that executes the script file and waits until execution is completed.

static void RunScript(string scriptFileCmd)
{
     Process proc = new Process(); 
     proc.StartInfo.FileName = "cscs.exe";
     proc.StartInfo.Arguments = "/nl"  + scriptFileCmd;
     proc.StartInfo.UseShellExecute = false;  
     proc.StartInfo.RedirectStandardOutput = true;  
     proc.StartInfo.CreateNoWindow = true;   
     proc.Start();
 
     string line = null;    
     while (null != (line= proc.StandardOutput.ReadLine()))
     {
          Console.WriteLine(line);
     }
     proc.WaitForExit();
}

This method is implemented in C#, however the similar implementation can be done in almost any programming language.

 

See Also

Reference | Installation