English 中文(简体)
Limit the Number of Created Processes
原标题:
  • 时间:2009-11-12 17:19:23
  •  标签:
  • c#
  • process

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 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.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签