c#中的控制台app关闭框等效命令 [英] Console app close box equivalent command in c#

查看:79
本文介绍了c#中的控制台app关闭框等效命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我继承了一个用C#编写的控制台应用程序,单击控制台关闭按钮时会很好地退出。



我试图复制代码中的关闭按钮功能,但我一直遇到内部代码错误。为什么控制台关闭按钮工作但代码不工作?



关闭框实际上对应用程序做了什么?



好​​的,似乎我需要更好地解释一下。

应用程序在可见的控制台屏幕上运行,状态数据显示和更新。

该应用程序非常复杂和庞大。它有线程运行所以证明很难在代码中关闭加C#不是我的事情虽然我糊里糊涂。

尽管线程运行等等,点击控制台右上角关闭框很好地退出应用程序没有任何错误。



我希望在代码中复制控制台关闭操作,这样我就可以实现午夜关机。

使用Environment.Exit(0)或Application.Exit会引发很多与线程有关的错误。

如果控制台窗口可以没有错误地退出那么为什么我不能在代码中转出应用程序?我意识到这不是一个解决问题的好方法,但我需要一个快速而肮脏的解决方案,同时我会更好地理解事情并在以后做好。



希望这有助于解释一下。



我尝试过:



我试过

Hi
I have inherited a console app written in C# that exits nicely when you click the console close button.

I'm trying to replicate that close button functionality in code but I keep running into internal code errors. Why does the console close button work but code does not?

What does the Close Box actually do to a app?

Ok, seems I need to explain a bit better.
The app operates in a visible console screen with status data being displayed and updated.
The app is quite complicated and large. It has threads running so proving quite difficult to shut down in code plus C# is not my thing although I muddle through.
Despite the threads running etc etc, clicking the console top right Close Box nicely quits the app without any errors.

I wish to replicate the console shutdown action in code so I can implement a midnight shutdown.
Using Environment.Exit(0) or Application.Exit throws lots of errors to do with threads.
If the console window can exit without errors then why can't I dump out of the app in code? I realise this is not a good way to approach the problem but I need a quick and dirty solution whilst I work on understanding things better and do it right later on.

Hope this helps explain a bit better.

What I have tried:

I've tried

Environment.Exit(0);

Application.Exit()

推荐答案

控制台应用程序在程序完成时结束,试试这个;

a)打开Visual Studio&创建一个新的控制台应用程序

b)在Main方法中输入以下内容

Console applications end when the program completes, try this;
a) Open Visual Studio & create a new Console application
b) Enter the following in the Main method
static void Main(string[] args)
{
    Console.WriteLine("Hullo World");
}



c)编译应用程序

d)浏览Windows和Windows中的可执行文件双击 - 控制台窗口将出现并消失。

这是控制台应用程序的工作方式。

如果您通过命令提示符运行它,您将看到文字Hullo World&你会回到命令提示符。

如果你想让应用程序等到你做某事就编辑上面的代码如下;


c) Compile the application
d) Browse to the executable in Windows & double-click - a console window will appear and disappear.
This is how a console application works.
If you were to run it via the command prompt you will see the text "Hullo World" & you would return to the command prompt.
If you want the application to wait until you do something edit the above code as follows;

static void Main(string[] args)
{
    Console.WriteLine("Hullo World");
    Console.WriteLine("Press any key to exit...");
    Console.ReadyKey();
}



现在,当您通过双击运行它时,应用程序将显示您的控制台窗口&然后等到你在关闭前按任意键。

如果你在命令提示符下运行它仍然会看到Hullo World和按任意键退出,你需要按一个键返回命令提示符。



调试现有的控制台应用程序以查看它为什么不自行关闭



亲切的问候


Now when you run it via double-click the application will display your console window & then wait until you press any key before closing.
If you run it at the command prompt you will still see "Hullo World" and "Press any key to exit" and you will need to press a key to return to the command prompt.

Debug your existing console application to see why it is not "closing" itself

Kind Regards


你的申请有问题。



点击角落里的关闭按钮控制台窗口不会很好地关闭您的应用程序。这恰恰相反。它会突然停止您的应用程序并释放它占用的内存。这可能会导致资源泄漏,因为并非所有内容都以良好的方式发布。



您的工作应该是修复代码问题而不是尝试复制一些比坏事更糟糕的东西。
You've got problems with your application.

Clicking the Close button in the corner of the console window does NOT shutdown your app "nicely". It's quite the opposite. It stops your application abruptly and the memory it is occupying is freed. This can lead to resource leaks since not everything has been released in a nice manner.

Your work should really be on fixing the problems with the code, not trying to replicate something that's doing more bad than good.


这篇关于c#中的控制台app关闭框等效命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆