0

I have a method which calls external exe file.

 private void MainTask()
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "test.exe";
        process.StartInfo = startInfo;
        process.Start();
    }

It can take 10-50-150 seconds to run test.exe file. The question is how can I check where it was executed or is still running.

I was trying to implement backgroundWorker like this:

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        MainTask();
    }


    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

        pictureBoxOutput.Image = 
        MessageBox.Show("C Sharp is awesome.");
    }

Is it possible to check when method is completely executed? Because right now I get a message "C Sharp is awesome." even though process is still running.

Edit: I want to create a progress bar and I need to update few elements on my form after process finishes. Process creates new files and I need to display new files' names. Of course it should finish first otherwise application will crash

Almazini
  • 1,825
  • 4
  • 25
  • 48
  • 1
    Sounds like you want `process.WaitForExit();`? Did you research the problem? Typing `c# wait for process to finish` into your favourite search engine gives a lot of results... – Equalsk Feb 07 '18 at 15:40
  • Also worth having a look at: https://stackoverflow.com/questions/12273825/c-sharp-process-start-how-do-i-know-if-the-process-ended – Riscie Feb 07 '18 at 15:41
  • @Steve I am running external exe file ... which was written on C++ loong time ago but i need its functionality to modify some files – Almazini Feb 07 '18 at 15:43
  • You need to explain better what you want to do while waiting for the external process to reach its end. WaitForExit could not be the correct answer here. – Steve Feb 07 '18 at 15:44
  • @Steve I want to create a progress bar ... and I want to update few elements on my form after process finishes. – Almazini Feb 07 '18 at 15:45
  • 4
    How can you meaningfully create a progress bar? It's not like processes are forced to provide information to other processes that they're e.g. 50% complete. If the process does provide some form of output that does allow progress to be assessed, surely the question should be more oriented around *consuming that output* rather than trying to just detect that the process is over? – Damien_The_Unbeliever Feb 07 '18 at 15:48

3 Answers3

0

You need to call process.WaitForExit()

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
 startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
 startInfo.FileName = "test.exe";
 process.StartInfo = startInfo;
 process.Start();
 process.WaitForExit(); // this will block while the process is running
Mihail Shishkov
  • 14,129
  • 7
  • 48
  • 59
  • Will it freeze application? so I won't be able to create progress bar – Almazini Feb 07 '18 at 15:45
  • If you are building a desktop application, you must not start this on the UI thread. Start it on a new thread, show the progress bar and then when the thread finishes - stop the progress bar. This is very easy to do with async/await and Task Parallel Library (TPL) – Mihail Shishkov Feb 07 '18 at 16:52
0

It all boils down to having a callback function after the the programm is finished. There are two ways for this:

  1. Register the Process classes Exited Event.

  2. If you want to keep your current BackgroundWorker code, you have to add a "wait for the Process to finish befor you continue" call inside the BackgroundWorker, as Mihail Shishkov showed in his answer.

Process.Start() will not wait for the process to exit. It will continue instantly. Effectively using Process.Start() is the same as hitting the "Ok" button on the Windows Run Dialog. Your code will instantly continue, regardless what the programm does from then on. In most cases you even have the Reference to the process instance run out of scope instantly after calling start.

Christopher
  • 9,634
  • 2
  • 17
  • 31
0

You can use the Exited event

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "test.exe";
process.StartInfo = startInfo;
process.Exited += (sender, e) => {
    pictureBoxOutput.Image = ...;
    MessageBox.Show("C Sharp is awesome.");

};
process.Start();
Yitzchok
  • 691
  • 3
  • 18