Tag: 控制台应用程序

Console.ReadKey()与multithreading的奇怪行为

在multithreading程序中使用Console.ReadKey()时,我遇到了一个奇怪的问题。 我的问题是:为什么会这样? 这是一个错误,还是因为我滥用Console ? (请注意, 根据文档 ,控制台应该是线程安全的 。) 用代码解释这个是最容易的: using System; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplication2 { internal class Program { private static void Main(string[] args) { Console.WriteLine(“X”); // Also try with this line commented out. Task.Factory.StartNew(test); Console.ReadKey(); } private static void test() { Console.WriteLine(“Entering the test() function.”); Thread.Sleep(1000); Console.WriteLine(“Exiting the test() function.”); } […]