简体   繁体   中英

Prevent Program to close when the process exit C#

I have a batch file that i run through process but when the batch file finish running and closed the Form also close. How can i prevent the process in closing the form. Here is the code.

Process support = new Process();
                        support.StartInfo.FileName = @"C:\Support\Support.bat";
                        support.EnableRaisingEvents = true;
                        support.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        support.StartInfo.CreateNoWindow = true;
                        support.StartInfo.UseShellExecute = false;
                        support.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
                        support.StartInfo.RedirectStandardOutput = true;
                        support.Start();
                        support.BeginOutputReadLine();

Here is the output handler that writes the output of the batchfile to a richTextbox

private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
        string line;
        line = (outLine.Data.ToString());
        txtStatus.AppendText(line + "\n");

    }

The batch file automatically close when done and the Program close also and never executed the remaining codes.

You can use either

Console.ReadLine();

or

Console.ReadKey();

ReadLine() waits for the Enter key to be pressed, while ReadKey() waits for any key to be pressed (Except modifier keys - Shift, Control, Alt, Command, Option.. to name a few)

If this is purely for testing from VS then you can run the app without debugging using Ctrl + F5

Process will open up a window by itself, and once the .bat file exits, will exit and close the main window. It's not your code that is wrong, it's by default the way a .bat file is run if you just double-clicked it in Explorer (cmd opens until .bat is completed and then closes again)

You mentioned that you tried "Process.WaitForExit()" but I'd like to point out, in your context, it should be "support.WaitForExit()" much like your "dt.WaitForExit()" (code you posted that is now removed)

If that doesn't work, add "PAUSE" on a new line in your support.bat file. This will prevent the window from closing as well until a key is pressed.

If you do not have access to edit the support.bat file, all you need to do is read the file contents and save it somewhere temporarily, and then run it... or add it:

using (StreamWriter w = File.AppendText("support.bat"))
{
  w.WriteLine("PAUSE");
}

Above is obviously an example, you'd need to check if "PAUSE" is already the last line before writing to the file again. I also didn't show code on how to create a file and saving it, you should be able to handle that :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM