C# 程序在运行其他进程后退出

发布于 2024-11-29 09:01:29 字数 840 浏览 0 评论 0原文

这个问题很奇怪。我以前从未遇到过类似的事情。 我正在尝试让我的程序使用 7zip 提取文件。我以前在其他程序中做过这个,而且从来都不是太困难。因此,我将代码复制并粘贴到:

Process process = new Process
{
    StartInfo =
    {
        CreateNoWindow = true,
        WindowStyle = ProcessWindowStyle.Hidden,
        FileName = "7za.exe",
        UseShellExecute = false,
        ErrorDialog = false,
        Arguments = "x -y -o\"" + outputPath +"\" \""+ inputFile +"\"",
        RedirectStandardOutput = false,
    }
};
process.Start();

此代码运行后,我的应用程序立即终止。它就这样消失了。这当然不是故意的!我使用了单步执行函数并运行了它。一旦 process.Start(); 完成,程序就会关闭并返回到 Visual C#。它没有运行任何 Application.Exit(); 或任何东西,它就消失了。 Visual C# 中没有任何错误在等待着我。 所以我尝试在末尾添加 MessageBox.Show("Test"); 。以单步模式运行它。 process.Start(); 没问题吗,一旦执行了 MessageBox 代码,它就又消失了。我什至没有在消息框中单击“确定”(该消息在应用程序终止之前显示了大约 0.2 秒)

This problem is bizarre. I have never encountered anything like it before.
I am trying to make my program extract a file using 7zip. I have done this before in other programs and it was never too difficult. So I copy and pasted my code in:

Process process = new Process
{
    StartInfo =
    {
        CreateNoWindow = true,
        WindowStyle = ProcessWindowStyle.Hidden,
        FileName = "7za.exe",
        UseShellExecute = false,
        ErrorDialog = false,
        Arguments = "x -y -o\"" + outputPath +"\" \""+ inputFile +"\"",
        RedirectStandardOutput = false,
    }
};
process.Start();

Immediately after this code has run my application terminates. It just disappears. It's certainly not meant to! I used the step into function and ran it. As soon as process.Start(); had finished the program closed and returned me into Visual C#. It didn't run any Application.Exit(); or anything, it just went away. There was no error awaiting me in Visual C#.
So I tried adding a MessageBox.Show("Test"); to the end. Ran it in step mode. Did process.Start(); fine, as soon as it executed the MessageBox code it disappeared again. I didn't even click ok in the message box (which showed up for about 0.2 seconds before the application terminated)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪之触 2024-12-06 09:01:29

如果这段代码位于 Main() 中,那么当您的代码执行完毕时,它将完成。您可以发布包含此片段的完整代码吗?

来等待该过程完成,

process.WaitForExit();

您可以通过添加检查 ExitCode 这可能会告诉您该过程是否成功。您也可以随时重定向 StandardError 来检查其输出。

process.StartInfo.RedirectStandardError = true;
string error = process.StandardError.ReadToEnd();

Well if this code is in Main() then it will finish when your code is done executing. Can you post the full code where this snippet is contained?

You can wait for the process to finish by including

process.WaitForExit();

Checking the ExitCode might tell you if your process succeeded. And you can always redirect the StandardError to check the output of that too.

process.StartInfo.RedirectStandardError = true;
string error = process.StandardError.ReadToEnd();
王权女流氓 2024-12-06 09:01:29

在 process.Start() 之后尝试 process.WaitForExit() ...

Try process.WaitForExit() after process.Start() ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文