开发者

Limit the Number of Created Processes

开发者 https://www.devze.com 2022-12-11 11:16 出处:网络
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 fil

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开发者_如何学C)
        {
            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 haven't 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消