中断到Console.ReadLine

编程入门 行业动态 更新时间:2024-10-26 10:35:36
本文介绍了中断到Console.ReadLine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以停止到Console.ReadLine()编程?

我有一个控制台应用程序的大部分逻辑在不同的线程运行,并在主线程我接受输入使用到Console.ReadLine()。我想停止从控制台读取时分离出的线程停止运行。

I have a console application: the much of the logic runs on a different thread and in the main thread I accept input using Console.ReadLine(). I'd like to stop reading from console when the separated thread stop running.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

这是可能的,你必须关闭标准输入流挺举的地板垫。此程序演示的想法:

It's possible, you have to jerk the floor mat by closing the stdin stream. This program demonstrates the idea:

using System; using System.Threading; using System.Runtime.InteropServices; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { ThreadPool.QueueUserWorkItem((o) => { Thread.Sleep(1000); IntPtr stdin = GetStdHandle(StdHandle.Stdin); CloseHandle(stdin); }); Console.ReadLine(); } // P/Invoke: private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 }; [DllImport("kernel32.dll")] private static extern IntPtr GetStdHandle(StdHandle std); [DllImport("kernel32.dll")] private static extern bool CloseHandle(IntPtr hdl); } }

更多推荐

中断到Console.ReadLine

本文发布于:2023-11-03 13:11:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Console   ReadLine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!