IngTun2018
IngTun2018

Reputation: 329

How to check that process is terminated in WCF c#

I have a WCF rest service, and inside it I hava a execution of a Process:

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = "/C " + Properties.Resources.NAME_APP_IAL + " " + ...);
        startInfo.WorkingDirectory = HttpContext.Current.Server.MapPath(@"" + ...);
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        Process oProcess = null;
        try
        {
            oProcess = Process.Start(startInfo);
            bool bStep = true;
            while (bStep)
            {
                Process[] oPro = Process.GetProcessesByName(Properties.Resources.NAME_APP_IAL);
                if (oPro == null || oPro.Count() == 0 ) bStep = false;
            }
        }

        catch (Win32Exception ex)
        {
            throw ..
        }

The process works well,

but after it finished I get a file as result ,

my problem that the process is terminated quickly so that I get empty file ! So how can I fix it : to be sure that the processus is finished or terminated at first ? Also with this verify if I have a exception or the process is blocked or anything else, how can I recognize it ?

Thanks for your help and for your suggestion and advice,

Upvotes: 1

Views: 70

Answers (1)

Murray Foxcroft
Murray Foxcroft

Reputation: 13785

You need to use WaitForExit. See dotnetperls.

   using (Process exeProcess = Process.Start(startInfo))
   {
       exeProcess.WaitForExit();
   }

Upvotes: 1

Related Questions