I want to scan a queue every 10 seconds using a Timer. If there are more then 0 items in this queue then Deque the first one, pass it as an argument to a Process and run that process. The timer should be disabled while this process executes. Once the process exits, it should re-enable the timer.
The items in the queue can be added manually or can come from a database.
The following C# code does not works after the first process is finished. For some reason the timer is not enabled again. Can somebody help?
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());
}
}