简体   繁体   English

单击MessageBox后,关闭C#控制台应用程序

[英]Close C# Console Application after MessageBox click

I'm still learning all the cool tricks C# has to offer. 我仍在学习C#提供的所有很酷的技巧。 I have a messagebox alert that pops up after a timer expires. 我有一个消息框警报,计时器到期后会弹出。 I'm wondering if it is at all possible to have the console application terminate on its own after the user click the "OK" button on the messagebox. 我想知道在用户单击消息框上的“确定”按钮之后,是否有可能让控制台应用程序自行终止。 The console app is automatically minimized to the taskbar if that of any circumstance. 在任何情况下,控制台应用程序都会自动最小化到任务栏。

Thanks in advance. 提前致谢。

Give this a go: 试试看:

// Terminates this process and gives the underlying operating system the specified exit code.
Environment.Exit()

MSDN: Environment.Exit Method MSDN:Environment.Exit方法

This is what I would have done. 这就是我要做的。

Wrap your messagebox code into an if statement 将您的消息框代码包装到if语句中

    if (MessageBox.Show("error", "error", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
    {
        System.Diagnostics.Process.GetProcessesByName("YOURPROCESSNAME.EXE")[0].Kill();
    }

There is no errorhandling in here, and I take it your console runs as a process. 这里没有错误处理,我认为您的控制台作为一个进程运行。 If you use the name of the process your console is running in GetprocessesbyName, you can close it with this method. 如果使用控制台名称在GetprocessesbyName中运行的进程的名称,则可以使用此方法将其关闭。

To blatantly kill your app (stop process) you can also use Environment.Exit(0) instead of Process.Kill(). 要公然杀死您的应用程序(停止进程),您还可以使用Environment.Exit(0)代替Process.Kill()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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