我想要使用计时器每10秒扫描一次队列。如果该队列中有多于0个项目,则将第一个项目作为Deque,将其作为参数传递给Process并运行该进程。在执行此过程时应禁用计时器。一旦进程退出,它应该重新启用计时器。进程队列计时器
队列中的项目可以手动添加或可以来自数据库。
以下C#代码在第一个进程完成后无法工作。由于某种原因,定时器不能再次启用。有人可以帮忙吗?
public MainForm()
{
InitializeComponent();
queue = new Queue<string>();
process = new Process();
process.Exited += new EventHandler(Process_Exited);
process.EnableRaisingEvents = true;
}
void StartProcess(string args)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"C:\Program Files\My Software\RunProcess.exe";
psi.Arguments = args;
psi.WindowStyle = ProcessWindowStyle.Minimized;
process.StartInfo = psi;
process.Start();
}
void Process_Exited(object sender, EventArgs e)
{
timer.Enabled = true;
}
void Timer_Tick(object sender, EventArgs e)
{
if (queue.Count > 0)
{
timer.Enabled = false;
StartProcess(queue.Dequeue());
}
}
该进程可以根据它必须处理的数据量运行可变的时间量。问题是它不能并行运行,这就是为什么我必须排队。这个过程所花费的时间可以在30秒到3小时之间。在此期间,我的软件也必须执行其他任务,所以不能使用WaitForExit()。 – 2010-08-23 07:56:07