0

I have two simple Console APPs. Program1 calls Program2 to do simple moving of 500 files to another folder.

Program1.exe:

    static void Main(string[] args)
    {
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        proc.EnableRaisingEvents = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardInput = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.FileName = @"D:\Program2.exe";
        proc.Start();
        proc.WaitForExit();
        proc.Close();
    }

Program2.exe:

    static void Main(string[] args)
    {
            string srcDic= @"D:\docs";
            var DesDic= @"D:\docs2";

            var pdf_files = Directory.GetFiles(srcDic, "*.pdf", SearchOption.TopDirectoryOnly);
            foreach (var pdf_file in pdf_files)
            {
                var fileName = new DirectoryInfo(pdf_file).Name;
                File.Move(pdf_file, DesDic + "\\" + fileName);
                Console.WriteLine("Moving " + pdf_file);
            }

            Console.WriteLine("----- All files moved!");
    }

Issue: When I run Program1.exe to run Program2, most of the time Program2 will be frozen and doesn't move all the files. I have to close it manually.

Note:

  • When I run Program2.exe without Program1, it works great and moves all the files.
  • I don't have Anti-Virus, and I even turned off the Windows Defender Firewall
  • This code is part of a big software, and I don't want to write the move code directly in Program1
Ribaz
  • 476
  • 3
  • 10
  • 2
    If you're redirecting StandardOutput you need to [read it before exiting](https://stackoverflow.com/a/1040730/22437). – Dour High Arch Apr 22 '21 at 19:58
  • Set proc.StartInfo.RedirectStandardOutput = false and try again. – slacker Apr 22 '21 at 20:19
  • @DourHighArch I had StandardOutput.ReadToEnd() but it was after .WaitForExit();. I moved it to before. I guess it fixed the issue, but I am not sure if that never happens again. – Ribaz Apr 22 '21 at 20:30
  • If you're not going to write to StandardInput or read from StandardOutput you shouldn't be redirecting them. – Dour High Arch Apr 22 '21 at 21:05

1 Answers1

0

Have You tried lowering the amount of files on a Test run for APP 2 ? Lower the amount of files in the Docs directory to 20 and see if it still freezes. Might also be timing issues. Paste a Thread.Sleep(); after this step just to see, I wouldn't use this in the final product. proc.StartInfo.UseShellExecute = false;

Becker218
  • 41
  • 5