我有两个类:Action类,它有一个执行VBScript文件的方法,以及包含Action实例列表的Item类。我的问题是我想限制可以同时运行的VBScript文件的数量。我没有这方面的经验,我用Google搜索并搜索过,但一无所获。我唯一想知道该怎么做的是这里:
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);
}
}
}
}
}
限制创建进程数量的要求对我来说似乎很常见,但正如我所说,我无法找到任何我可以构建的示例。我的问题是:这种常见或通常的做法是什么? (我也无法在StackOverFlow上找到任何样本,所以如果有,请发布链接)。欢迎任何提示或链接。
答案 0 :(得分:0)
你所拥有的将会奏效。
我不确定您无法找到更多信息的事实告诉您。
要么你正试图解决一个非问题 - 但如果你的脚本很大而且很复杂或需要访问共享资源,那么限制运行的数量似乎是一个好主意;或者说你的解决方案是正确的,并且没有其他人想过要提高它的解决方案。