67

Hi I am trying to do the following: I have a process which can take parameters (digits) and return the sum of these numbers

Process P = Process.Start(sPhysicalFilePath, Param);
                int result = P.ExitCode;

I get the return value from "ExitCode" the problem is: the program sometimes finishes his work before the process so when the program reaches this line

int result = P.ExitCode;

I got an exception .. my question is how to wait this process until it finishes its work sorry I forget to say that's I am working with C# language

Hany
  • 1,146
  • 4
  • 18
  • 26

2 Answers2

123

use:

Process P = Process.Start(sPhysicalFilePath, Param);
P.WaitForExit();
int result = P.ExitCode;

from MSDN

snicker
  • 6,080
  • 6
  • 43
  • 49
-1

You can try the below code:

    Dim P As New Process
    P = Process.Start(info)
    P.WaitForExit()
    fichiersATraiter = P.ExitCode

Hope this helps :)

Software Dev
  • 5,368
  • 5
  • 22
  • 45
Bahaa J.
  • 17
  • 4
  • 1
    OP asked for C# not VB ;) So you should put a disclaimer saying not C# – Niels Schmidt Nov 17 '18 at 22:31
  • In this example, 'fichiersATraiter ' wont work. – Vandrey Dec 21 '20 at 13:32
  • 1
    I don't know which was the greater waste of time: me seeing VBA code in the year 2021 or me googling a bit because I wanted to know more about this traitor Fichier. Both distractions yielded no benefit. – Wellspring Aug 20 '21 at 14:09