tags:

views:

41

answers:

1

I have two classes: Action class, that has a method for executing VBScript files, and Item class that contains a list of Action instances. My problem is that I want to limit the number of VBScript files that can be run at the same time. I have no experience with this, and I have googled and searched around, but found nothing. My only idea of how to do is is presented here:

using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;

namespace Test
{
    public class Action
    {
        public string Script;
        public static int Limit;
        public static int ActiveCount = 0;

        public Process process = new Process();

        public Action(string script)
        {
            Script = script;
        }

        public void Execute()
        {
            process.EnableRaisingEvents = true;
            process.Exited += new EventHandler(Handler);

            try
            {
                if (ActiveCount < Limit)
                {
                    process = Process.Start(
                         "c:\\windows\\system32\\wscript.exe",
                         "\"" + Script + "\"");
                    ActiveCount++;
                }
            }
            catch(Win32Exception e)
            {

            }
        }

        private void Handler(
            object sender, EventArgs e)
        {
            ActiveCount--;
        }
    }

    public class Item
    {
        public ArrayList Actions = new ArrayList();
    }

    class Program
    {
        static void Main()
        {
            Action.Limit = 5;

            Item item = new Item();


            item.Actions.Add(
                new Action("C:\\Scripts\\Test_1.vbs"));

            for (int i = 0; i < 10; i++)
            {
                foreach (Action action in item.Actions)
                {
                    action.Execute();
                    Console.WriteLine(Action.ActiveCount);
                }
            }
        }
    }
}

The requirement of limiting the number of created processes seems common to me, but as I said, I havent been able to find any samples I could build on. My question is: what is the common or usual way of doing this? (I also haven't been able to find any samples here on StackOverFlow, so if there are any, please post the link). Any hint or a link is welcome.

Thanks in advance.

+1  A: 

Well what you've got will work.

I'm not sure what the fact that you can't find more information tells you.

It's either that you're trying to solve a non-problem - but if your scripts are large and complex or need access to shared resources then limiting the number that run would seem to be a good idea; or it's that your solution is the right one and it's so trivial no one else has thought to raise it.

ChrisF
Thank you for the comment. The idea was to make it possible for the user to limit the process count to add some flexibility. I guess I'll have to do some testing.
Uros Calakovic