system.diagnostics.Process in a loop

Spunny 326 Reputation points
2023-07-19T19:26:48.77+00:00

Hi,

I have bund of pdf files in a folder. They have naming convention. I need to use system.diagnostics.process to use third party vendor exe to create combined pdf out of those files. I have criteria to loop through.

Here is my code:

foreach (string st in stringlist)

{

string arguments = @"C:\Files\ + CompanyName + "_" + BranchName + "_*.pdf cat output " + @"C:\\Files\+ CompanyName + "_" + BranchName + "_Combined-" + pYear + ".pdf";
                ProcessStartInfo info = new ProcessStartInfo(pPdfUtility);                
                info.Arguments = arguments;
                info.RedirectStandardError = true;
                info.RedirectStandardOutput = true;
                info.CreateNoWindow = true;
                info.UseShellExecute = false;

                Process newProcess = Process.Start(info);
                if (newProcess != null && !newProcess.HasExited)
                    newProcess.Kill();   

}

When I run in debug step into, if I give gap, then working. Otherwise nothing happening. Do I need to give process to finish 30 seconds or something before it goes into the loop. It's never going into kill line.

Please let me know how to do it

I am not doing async, await etc. This is old application. If I need to use async and await, need to modify method signature which gets called and entire chain has to be converted to async.

Thanks,

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,296 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,751 Reputation points
    2023-07-19T19:38:58.0866667+00:00

    Once you've started the process with Process.Start you need to wait for the program to exit like this:

    newProcess.WaitForExit();
    

    You shouldn't need to manually .Kill() the process unless it needs to be interrupted.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.