1

I have problem with command line window after executing bat file. I want to run bat file, without cmd window shown, close it and after all this open my PDF file which is generated using my program and bat file. My code:

 Process p1 = new Process();
            p1.StartInfo.FileName = @"C:\dir\batch.bat";
            p1.StartInfo.Arguments = @"C: \dir\final.tex";
            p1.StartInfo.UseShellExecute = false;
            p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;


            p1.Start();
            p1.WaitForExit(15*1000);
            p1.Close();

            Process.Start(@"C:\dir\final.pdf");

So what happens in my case: Everything runs as it should do, but my PDF is show before my bat file is doing the job. How to make my Process.Start(pdf) wait, till my bat file will finish the job? When using p1.WaitForExit(), I need manually exit the cmd window, I don't want to do this. Any ideas?

Thanks.

My bat file:

xelatex --output-driver="xdvipdfmx -V 7 -q" final.tex
xelatex --output-driver="xdvipdfmx -V 7 -q" final.tex
call clean.bat
Mantas
  • 199
  • 2
  • 12

1 Answers1

1

You are looking for this one:

        p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

Note that MSDN says that

To use ProcessWindowStyle.Hidden, the ProcessStartInfo.UseShellExecute property must be false.

which you do have there, but just in case ...

ProcessStartInfo.CreateNoWindow is something else - this is about whether your process window will host the new process window. The default for this is false, which is what you want, so do not set it to true. I.e. remove

       p1.StartInfo.CreateNoWindow = true;

from your code. You do want the new process to run in its own window, but you want it to be a hidden window. Then you just need to wait for the termination of the new process and it's done.

To wait, you can use WaitForExit. However, I would always recommend you not to wait indefinitely, but always think about a sane limit - could be 30 seconds, 1 minute, 2 minutes ... depends on the task of your child process. And if it takes too long (the child process can be bugged and just hang from unknown reason) then kill it.

Update (using WaitForExit):

The following will wait at most 60 seconds for the child process to finish:

      if (p1.WaitForExit(60 * 1000))
      {
        // Child process finished, do whatever you want
      }
      else 
      {
        // Child process failed to finish within 60 seconds, so we kill it.
        // It is unlikely that there is a result you are expecting ...
        p1.Kill();
      }
Wapac
  • 4,058
  • 2
  • 20
  • 33
  • Maybe you could tell me how to make a limit , I bat file is runing about 5-10secs – Mantas Nov 23 '15 at 09:13
  • WaitForExit function has optional arguments. So you go like if (p1.WaitForExit(60 * 1000)) { EVERYTHING OK } else { p1.Kill(); } this code will wait for at most 1 minute and if child fails to finish within those 60 seconds it is getting killed. – Wapac Nov 23 '15 at 09:14
  • Thank for your answer, but if I use p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; , Console window appears and If i use createnowindow it works right. Maybe you can comment about that? – Mantas Nov 23 '15 at 09:30
  • How does your code look like now? Did you remove CreateNoWindow = true? Also the problem may be in your .bat file. Can you share its contents? It might be the case that the bat file it is running the new process in a new window, which is not hidden. – Wapac Nov 23 '15 at 09:33
  • I added my batch file and edited my code – Mantas Nov 23 '15 at 09:35
  • What if you remove "call clean.bat", still same issue? – Wapac Nov 23 '15 at 09:56
  • Yes, I opens pdf generating commands. So it's not hiding cmd window at all. – Mantas Nov 23 '15 at 10:02
  • OK, can you try changing StartInfo.FileName to "C:\WINDOWS\system32\cmd.exe" and StartInfo.Arguments to "/C C:\dir\batch.bat C:\dir\final.tex". Does it make any difference? – Wapac Nov 23 '15 at 10:09
  • It makes different, now cmd window is hidden, but it is not using any bat files command at all :) – Mantas Nov 23 '15 at 10:22
  • That will be possibly because of working directory. Try adding the following as the very first line of your .bat file: cd %~dp0 – Wapac Nov 23 '15 at 10:32
  • It still not using bat. But is it very wrong to use `p1.StartInfo.CreateNoWindow = true;` ? – Mantas Nov 23 '15 at 10:45
  • Using CreateNoWindow can be good as well as bad, it depends entirely on the context of your application and what you are trying to achieve it. If you run command line - cmd.exe - and then you run e.g. ping.exe from it, the ping.exe process will be started in the same window - i.e. its output will be written directly to cmd.exe's window, which is a good thing because that is what the user wants. The user does not want a new window to appear and then be closed. That is where you use CreateNoWindow. But for things like background processing, it is usually wrong. – Wapac Nov 23 '15 at 11:48
  • one more thing you can try is p1.StartInfo.WorkingDirectory = "c:\dir" – Wapac Nov 23 '15 at 11:49