当我用Control-C中断C#控制台应用程序时会发生什么? [英] What happens when I interrupt a C# console application with Control-C?

查看:268
本文介绍了当我用Control-C中断C#控制台应用程序时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用Control-C中断C#控制台应用程序时会发生什么?

What happens when I interrupt my C# console application with Control-C?

该进程被杀死了吗?内存释放了吗?是否执行了finally块?数据库连接会发生什么?

Is the process killed? Is memory freed? Are finally blocks executed? What happens to database connections?

如果该应用程序是为调试或发布而构建的,或者是在Visual Studio的内部/外部运行的,那么这是否有所不同?

Does any of this differ if the application is built for debug or release, or run inside/outside Visual Studio?

推荐答案

简短答案:在CTRL-C之后没有任何作用

Short Answer: It's doing nothing after CTRL-C

长答案:

Long Answer: There is good article about it on the MSDN which clearly states, that it sends a signal (interrupt) instead of keypress-events.

还有一个 cancelKeyPress-Event 触发后,您可以订阅并做任何您想做的事!

There is also a cancelKeyPress-Event triggered which you can subscribe to and do whatever you want!

不幸的是,没有更多关于默认情况下实际完成操作的信息.也许在最坏的情况下,您可以自己检查一下.但是imo应该有一些关于它的文档...

Unluckily there is no more info about what's actually done by default. Maybe in the worst case you can check it out by yourself. But imo there should be some documentation about it...

更新:Alois Kraus编写了 codeproject- 关于收到CTRL-C后正常关闭控制台应用程序的文章.

UPDATE: Alois Kraus wrote a codeproject-Article about gracefully shuting down a console-application after receiving CTRL-C.

引用Alois Kraus:

To quote Alois Kraus:

CLR的默认行为是不执行任何操作.这的确意味着DLL_PROCESS_DETACH通知在很晚之后通知了CLR,在该通知中,由于已经使用了OS加载程序锁,因此不再有托管代码可以运行.不幸的是,我们没有收到任何通知事件,也没有任何终结器运行.所有线程都被静默杀死,没有机会执行其catch/finally块以有序关闭.我在第一句话默认"中说过,因为有一种方法可以很好地处理这种情况. Console类在.NET 2.0中有了一个新的事件成员:Console.CancelKeyPress.它使您可以收到Ctrl-C和Ctrl-Break键的通知,您可以在其中停止关闭(仅适用于Ctrl-C,但不适用于Ctrl-Break).这里的主要问题是,如果您捕获Ctrl-C/Break事件并退出处理程序,则不会调用终结器.这不是我所说的合作关闭.我想到的第一件事是调用Environment.Exit,但它不会触发任何终结器.一切都不会丢失.我确实想出了一个肮脏的技巧来运行所有终结器:我们在事件处理程序中启动了一个小的帮助程序线程,该线程随后将调用Environment.Exit.瞧,我们的终结器叫做.

The default behavior of the CLR is to do nothing. This does mean that the CLR is notified very late by a DLL_PROCESS_DETACH notification in which context no managed code can run anymore because the OS loader lock is already taken. We are left in the unfortunate situation that we get no notification events nor are any finalizers run. All threads are silently killed without a chance to execute their catch/finally blocks to do an orderly shutdown. I said in the first sentence default, because there is a way to handle this situation gracefully. The Console class has got a new event member with .NET 2.0: Console.CancelKeyPress. It allows you to get notified of Ctrl-C and Ctrl-Break keys where you can stop the shutdown (only for Ctrl-C, but not Ctrl-Break). The main problem here is if you catch the Ctrl-C/Break event and exit the handler there are no finalizers called. This is not what I would call a cooperative shutdown. The first thing that comes to my mind is to call Environment.Exit but it does not trigger any finalizers. All is not lost. I did come up with a dirty trick to run all finalizers: we spin up a little helper thread inside the event handler which will then call Environment.Exit. Voila, our finalizers are called.

这篇关于当我用Control-C中断C#控制台应用程序时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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