Run processes sequentially within BackgroundWorker

hex02

New member
Joined
Aug 22, 2017
Messages
3
Programming Experience
Beginner
Having trouble running multiple processes one after another from within backgroundworker

First processes, regardless which one I put as first seems to run, however nothing occurs afterwards.

Please excuse my ignorant, I cant seem to find what I am looking for,
Wondering if someone can shed some light on

wether I am using wrong method or is it something simple I can't see

Here is a section of the code:
 private BackgroundWorker BackgroundWorker = new BackgroundWorker();

    public Form1()
    {
        InitializeComponent();

        BackgroundWorker.WorkerSupportsCancellation = true;
        BackgroundWorker.WorkerReportsProgress = true;
        BackgroundWorker.DoWork += BackgroundWorker1_DoWork;
        BackgroundWorker.ProgressChanged += BackgroundWorker1_ProgressChanged;
        BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker1_RunWorkerCompleted);

    }

    private void Button1_Click(object sender, EventArgs e)
    {

        if (!BackgroundWorker1.IsBusy)
        {
            BackgroundWorker1.RunWorkerAsync();
        }
 else
        {
            label2.Text = "Busy processing, please wait";
        }
    }

private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

 {
            if(!File.Exists("proc1.ps1"))
            File.WriteAllBytes("proc1.ps1", SOL.Properties.Resources.proc1);
            if (!File.Exists("proc2"))
            File.WriteAllBytes("proc2", SOL.Properties.Resources.proc2);
            if (!File.Exists("proc3"))
            File.WriteAllBytes("proc3", SOL.Properties.proc3);

        Process proc1 = new Process();
        proc1.StartInfo.FileName = @"proc1.exe";
        proc1.StartInfo.Arguments = (@"–ExecutionPolicy Bypass ""proc.ps1""");
        proc1.Start();
        proc1.WaitForExit();


        Process proc2 = new Process();
        pro2.StartInfo.FileName = "proc2.exe";
        pro2.Start();
        pro2.WaitForExit();


        Process proc3 = new Process();
        pro3.StartInfo.FileName = "proc3.exe";
        pro3.Start();
        pro3.WaitForExit();
 
Firstly, I have fixed the formatting in your post. Please use formatting sparingly, for emphasis only. Making an entire post bold simply makes it harder to read. The default font is the default for a reason. I have also wrapped your code snippet in appropriate formatting tags. As you can see, it's much easier to read that way.

Secondly, to be clear, you don't run anything within a BackgroundWorker. The BackgroundWorker simply raises its DoWork event on a secondary thread and executes the method you tell it to. As you can see, that DoWork event handler is part of your form. You're running that code in your form, not in the BackgroundWorker.

As for the issue, I suggest that you debug your code. Place a breakpoint at the top of the DoWork event handler and then step through it line by line. I suspect that you'll find that every line is hit, assuming that the first process you start actually does exit. I can't see anything inherently wrong that code. I'd suggest either the first process doesn't exit or the subsequent processes are started fine but simply don't behave as you expect them to.
 
Firstly, I have fixed the formatting in your post. Please use formatting sparingly, for emphasis only. Making an entire post bold simply makes it harder to read. The default font is the default for a reason. I have also wrapped your code snippet in appropriate formatting tags. As you can see, it's much easier to read that way.

Secondly, to be clear, you don't run anything within a BackgroundWorker. The BackgroundWorker simply raises its DoWork event on a secondary thread and executes the method you tell it to. As you can see, that DoWork event handler is part of your form. You're running that code in your form, not in the BackgroundWorker.

As for the issue, I suggest that you debug your code. Place a breakpoint at the top of the DoWork event handler and then step through it line by line. I suspect that you'll find that every line is hit, assuming that the first process you start actually does exit. I can't see anything inherently wrong that code. I'd suggest either the first process doesn't exit or the subsequent processes are started fine but simply don't behave as you expect them to.

Thank you for fixing the format. I have removed WaitForExit, however still same results (executing first process only).


Your background worker will still run your processes synchronously, it just does it on another thread (not your UI thread) and since each of your processes waits for exit, that is exactly what the thread will do for each process. If you want to run all three processes asynchronously look at using Tasks.

https://blogs.msdn.microsoft.com/pfxteam/2011/10/24/task-run-vs-task-factory-startnew/


I would like the processes to run one after another, meaning when first processes is finished the second to begin etc.
 
I have removed WaitForExit, however still same results (executing first process only).
At no point did I suggest doing that. What I suggested was debugging the code. Have you done that?
I would like the processes to run one after another, meaning when first processes is finished the second to begin etc.
The code you posted should do exactly that. The only way it could not is if the first process doesn't exit or an exception is thrown. The way to determine if one of those is happening and which one is to debug. That's why VS has a debugger.
 
At no point did I suggest doing that. What I suggested was debugging the code. Have you done that?

Okay my apologies I must of misunderstood you.

The code you posted should do exactly that. The only way it could not is if the first process doesn't exit or an exception is thrown. The way to determine if one of those is happening and which one is to debug. That's why VS has a debugger.

I have been looking into the debugging and its complaining about missing/unable to open PDB files.

I came to the conclusion that the purpose of backgroundworker is to run with no user interactions, however 2 of the processes is running .exe's and they would require user interactions as input is different for each user, what do you suggest I need to include to the processes in order to have user interaction ?
 

Latest posts

Back
Top Bottom