简体   繁体   English

如何在C#中实现像Ctrl+C这样的按键中断的异常代码

[英]How to implement a exception code for key Interrupt like Ctrl+C in C#

I need your help in implementing an exception code with 'try ~ catch' in C#.我需要你的帮助来在 C# 中使用“try ~ catch”来实现异常代码。

My motive is to follow 'try ~ except' in python like below:我的动机是在 python 中遵循“try ~ except”,如下所示:

try: 
    pass 

except KeyboardInterrupt as e: 
    print("Closed...") 
    break 

I thought there was also the interrupt exception keyword like Python in C#.我认为 C# 中也有像 Python 这样的中断异常关键字。
So, I tried like:所以,我试过:

try 
{
    // pass 
}
catch (ThreadInterruptedException e) 
{
    Console.Write("Closed...");
    break;
}

But, this C# code couldn't catch the interrupt signal by ctrl+C.但是,这个 C# 代码无法通过 ctrl+C 捕获中断信号。
I assumed 'ThreadInterruptedException' is for that, but it isn't.我假设 'ThreadInterruptedException' 是为了那个,但它不是。

Which keyword should I use?我应该使用哪个关键字?

I have tried to google, but there are a number of examples about 'how to get 'ctrl+v' value through my console.我试过谷歌,但有很多关于“如何通过我的控制台获取‘ctrl+v’值的例子。

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

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