-1

I have three commands to execute waiting for each command to finish before starting the next one.

Based on my implementation after completing the first one the second one will start but backgroundWorker1_RunWorkerCompleted won't raise at all.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cmd_commands
{
    public partial class Form1 : Form
    {
        string[] commands = new string[] {
         @"test",
         @"test1",
         @"test2" };

        int command = 0;

        public Form1()
        {
            InitializeComponent();
        }

        public void runCmd(string command)
        {
            ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");
            cmdsi.Arguments = command;
            Process cmd = Process.Start(cmdsi);
            cmd.WaitForExit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            runCmd(commands[command]);
            backgroundWorker1.ReportProgress(0, command);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            label1.Text = "Working on command number: " + e.UserState.ToString();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
          command++;
          runCmd(commands[command]);
        }
    }
}
Daniel B
  • 3,109
  • 2
  • 33
  • 42
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120

1 Answers1

0

BackgroundWorker is one time usage only i.e once the state is Completed it wont restart, u need to re-instantiate it.

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    command++;

    if (command < commands.Length)
    {
        backgroundWorker1 = new BackgroundWorker();
        backgroundWorker1.DoWork += this.backgroundWorker1_DoWork;
        backgroundWorker1.ProgressChanged += this.backgroundWorker1_ProgressChanged;
        backgroundWorker1.RunWorkerCompleted += this.backgroundWorker1_RunWorkerCompleted;
        backgroundWorker1.RunWorkerAsync();
    }
}
Daniel B
  • 3,109
  • 2
  • 33
  • 42
  • I know that, My question is how do i check/know when the command prompt command has exited. I'm doing in the dowork: cmd.WaitForExit(); but what will prevent the backgroundworker to start over and execute the next command while the process of the first command has not finished/exited yet ? – Daniel Lip Oct 23 '17 at 00:57
  • once the command prompt exits the next statment after cmd.WaitForExit() will execute. in yr case there's nothing so it'll go to another event which is backgroundWorker1_ProgressChanged and then backgroundWorker1_RunWorkerCompleted – Daniel B Oct 23 '17 at 01:03