Is WaitForExit really waiting for the external program to finish?

I am writing an application that has two calls to external programs, RichCopy and 7zip. The idea is to use RichCopy to move files and 7zip to archive and encrypt files after RichCopy is complete. The problem I'm running into is that the app doesn't wait for RichCopy to finish moving the files before 7zip tries to zip them, despite the fact that I'm using WaitForExit. Code below:

file_copy(groupNumberINT, groupNumber, extFolderPath, scanFolderPath);
encrypt_data(groupNumber, outputFolder);

    private void file_copy(int groupNumberINT, string groupNumber, string externalFolder, string scansFolder)
    {
        if (groupNumberINT < 370)
        {
            string sourceFolder = "D:\\Test\\Production\\CMSFILE001-Copy\\" + groupNumber;

            ProcessStartInfo f001 = new ProcessStartInfo();
            f001.FileName = "C:\\Program Files (x86)\\Microsoft Rich Tools\\RichCopy 4.0\\RichCopy.exe"; //Edit in prod
            f001.Arguments = sourceFolder + " " + externalFolder;
            f001.WindowStyle = ProcessWindowStyle.Normal;

            Process f1 = Process.Start(f001);
            f1.WaitForExit();
        }
    }

    private void encrypt_data(string groupNumber, string outputDirectory)
    {
        // Create 7zip encrypted archive
        string archiveName = groupNumber + @".7z";
        string archiveFolder = @"D:\Test\" + groupNumber;
        string outputFile = tbGroupNumber.Text + ".7z";

        ProcessStartInfo p = new ProcessStartInfo();
        p.FileName = "C:\\Program Files\\7-Zip\\7za.exe";
        p.Arguments = "a -mx -mhe -pPassword fileout.7z folder";
        p.WindowStyle = ProcessWindowStyle.Maximized;

        Process x = Process.Start(p);
        x.WaitForExit();
    }

      

So RichCopy starts, but immediately after I see the splash screen for RichCopy 7zip starts archiving and encrypting an empty folder. Is there something I'm missing or should the WaitForExit () method wait for the process to complete before moving on to the next line of code?

+3


source to share


3 answers


Wait until the exit completes the external process.

My guess is that the first richcopy executable you run is probably starting another process, which then does the actual copy.



Working example Wait for exit:

using System.Diagnostics;

public class MainApp
{

    public static void Main(string[] args)
    {    
        string textFile = @"c:\workspace\1.txt";
        openNotepad(textFile);
        openNotepad(textFile);    
    }

    private static void openNotepad(string textfile)
    {         
         ProcessStartInfo f001 = new ProcessStartInfo();
         f001.FileName = "notepad.exe";
         f001.Arguments = textfile;
         f001.WindowStyle = ProcessWindowStyle.Normal;
         Process f1 = Process.Start(f001);
         f1.WaitForExit();    
     }  
}

      

+1


source


Try the following:

  • instead of void file_copy (), return the process. The returned process is f1.
  • to encrypt_data, add the Process argument and set this to the process you are returning from file_copy.
  • then in encrypt_data wait for the process to finish before you start the zip process.


I think what is happening is that WaitForExit in file_copy is not blocking for some reason. Either it has been optimized because nothing is behind it, or it is waiting, but the next line of the calling function runs anyway.

Another possibility is that the file_copy file starts RichCopy with malformed arguments, causing it to terminate early (either because it does nothing, or because it returns an error). Try running file_copy separately to make sure it works correctly and does what you expect.

0


source


For those who come across this question in the future, what I ended up doing was to wrap a while loop around the check to make sure the "RichCopy64" process is still running. If it still works, I told him to wait 10 seconds, then try again.

Thanks to everyone who replied - when I get a sufficient reputation, I will definitely reply with your answers.

0


source







All Articles