Just a Simple Parallel Runner in C#


I need a tool to run multiple scripts (mainly vbscript or jscript files on windows) at the same time (in parallel) so I decide to write one (it is ok to re-invent the wheel sometimes)…

d1f715f3671e731f75546ddf5230dd35 Just a Simple Parallel Runner in C# c # parallel computing software development software download Task Parallel Library tools / utilities

On Linux and Windows, you’ll be able to run multiple tasks at the same time using the & one command followed another like this:

1
echo task1 & echo task2 & echo task3
echo task1 & echo task2 & echo task3

Please note that, on Linux, the & actually puts a command to the background so the output sequence can be any combination (they do run in parallel). However, on Windows 10 command line shell, the above command always prints this:

1
2
3
task1
task2
task3
task1
task2
task3

So we can image, the & on windows works like a logical AND, one command finished and then another command follows (they are sequential and not in parallel). What about if we want to wait to make sure all tasks are finished before returning to the prompt? This is the motivation of ‘re-inventing’ the wheel.

Pre-compiled Binaries

The .NET 4.6.1 compiles the source code into 3 variants: AnyCPU, x86 and x64. All are zipped into THIS (only 14KB) aw Just a Simple Parallel Runner in C# c # parallel computing software development software download Task Parallel Library tools / utilities .

Features and Screenshots

A few examples:

SimpleRunner Just a Simple Parallel Runner in C# c # parallel computing software development software download Task Parallel Library tools / utilities

simplerunner

List the jobs one by one and they run in parallel

SimpleRunner-Examples Just a Simple Parallel Runner in C# c # parallel computing software development software download Task Parallel Library tools / utilities

simplerunner-examples

The SimpleRunner will wait after all jobs are completed. You could also set ‘timeout’ to kill a job if it takes more than a defined period (miliseconds).

The runners can be specified using the .ext=path format where ext is the job file extension. Also you can specify the parameters to pass for that runner, for example:

1
SimpleRunner .php=php.exe -php=/para1 -php=/para2 a.php b.php
SimpleRunner .php=php.exe -php=/para1 -php=/para2 a.php b.php

In this case, two processes will be invoked in parallel:

1
2
php.exe /para1 /para2 a.php
php.exe /para1 /para2 b.php
php.exe /para1 /para2 a.php
php.exe /para1 /para2 b.php

*.vbs and *.js are pre-configured with /Nologo parameter and they are interpreted using default cscript.exe. Make sure you specify the Runner path otherwise you will get a warning, which can be controlled by stopifwarning and printwarning.

1
2
3
4
5
6
# SimpleRunner.exe job1.vbs notfound.txt stopifwarning=true
Warning: notfound.txt Not Found.
stopifwarning = true
 
# SimpleRunner.exe job1.vbs notfound.txt stopifwarning=true printwarning=false
stopifwarning = true
# SimpleRunner.exe job1.vbs notfound.txt stopifwarning=true
Warning: notfound.txt Not Found.
stopifwarning = true

# SimpleRunner.exe job1.vbs notfound.txt stopifwarning=true printwarning=false
stopifwarning = true

Job Lists

There is a limit on the number of characters put in the command line, so as the number of jobs to run in parallel. Instead, you could have a jobs list, which is basically a text file, containing each line a job. For example:

1
2
3
job1.vbs
job2.vbs
job3.vbs
job1.vbs
job2.vbs
job3.vbs

Assume this is saved in jobs.txt and what you can do is simply adding jobs=jobs.txt, the SimpleRunner will read the file and add the jobs.

1
2
3
4
5
6
7
8
9
10
# SimpleRunner.exe jobs=jobs.txt
Process Starts: cscript.exe /NoLogo job1.vbs
Process Starts: cscript.exe /NoLogo job2.vbs
Process Starts: cscript.exe /NoLogo job3.vbs
1
Process Stops: cscript.exe /NoLogo job1.vbs
3
2
Process Stops: cscript.exe /NoLogo job3.vbs
Process Stops: cscript.exe /NoLogo job2.vbs
# SimpleRunner.exe jobs=jobs.txt
Process Starts: cscript.exe /NoLogo job1.vbs
Process Starts: cscript.exe /NoLogo job2.vbs
Process Starts: cscript.exe /NoLogo job3.vbs
1
Process Stops: cscript.exe /NoLogo job1.vbs
3
2
Process Stops: cscript.exe /NoLogo job3.vbs
Process Stops: cscript.exe /NoLogo job2.vbs

You can have multiple jobs=jobs.txt defined, for example, jobs=jobs1.txt jobs=jobs2.txt.

Complete Source Code

This is an open source, so feel free to make changes and create pull requests! I didn’t add other project files becuase it is so Simple! Here is the Github Project of SimpleRunner.

Launching Job as a Process

The core part is to launch the job task as a separate process, which can be easily accomplished by the following C# code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
private static void Run(string runner, string param, int timeout = -1)
{
    var startinfo = new ProcessStartInfo
    {
        FileName = runner,
        Arguments = param,
        WindowStyle = ProcessWindowStyle.Hidden,
        CreateNoWindow = true,
        UseShellExecute = false,
        RedirectStandardOutput = true
    };
    Console.WriteLine("Process Starts: " + runner + " " + param);
 
    using (var p = Process.Start(startinfo))
    {
        while (!p.StandardOutput.EndOfStream)
        {
            var line = p.StandardOutput.ReadLine();
            Console.WriteLine(line);
        }
        if (timeout < = 0) // no timeout
        {
            p.WaitForExit();
        }
        else
        {
            p.WaitForExit(timeout);
        }
    }
 
    Console.WriteLine("Process Stops: " + runner + " " + param);
}
private static void Run(string runner, string param, int timeout = -1)
{
    var startinfo = new ProcessStartInfo
    {
        FileName = runner,
        Arguments = param,
        WindowStyle = ProcessWindowStyle.Hidden,
        CreateNoWindow = true,
        UseShellExecute = false,
        RedirectStandardOutput = true
    };
    Console.WriteLine("Process Starts: " + runner + " " + param);

    using (var p = Process.Start(startinfo))
    {
        while (!p.StandardOutput.EndOfStream)
        {
            var line = p.StandardOutput.ReadLine();
            Console.WriteLine(line);
        }
        if (timeout < = 0) // no timeout
        {
            p.WaitForExit();
        }
        else
        {
            p.WaitForExit(timeout);
        }
    }

    Console.WriteLine("Process Stops: " + runner + " " + param);
}

Another multi-tasking job dispatcher is written purely in VBScript.

--EOF (The Ultimate Computing & Technology Blog) --

GD Star Rating
loading...
981 words
Last Post: How to Print MySQL Table Summary using PHP?
Next Post: How to Check If Your PHP code is 64-bit?

The Permanent URL is: Just a Simple Parallel Runner in C#

Leave a Reply