本文介绍了为什么Console.WriteLine的Console.ReadKey()块输出调用另一个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的控制台应用程序。

I have a very simple console app.

static void Main(string[] args)
{
    DoAsync();
    Console.ReadKey();
}

下面 DoAsync 开始设定的任务,并返回而不是等待任务的completition。每个任务写入到控制台,但关键是pressed前ouptut不显示。
当我使用到Console.ReadLine 一切工作正常。

Here DoAsync starts set of task and returns not waiting for tasks' completition.Each task writes to Console, but the ouptut is not shown before key is pressed.
When I use Console.ReadLine everything works fine.

所以,我很好奇 ReadKey() pecularities。

So I'm curious about ReadKey() pecularities.

推荐答案

的文档 Console.ReadKey()

的ReadKey方法等待,即,在该线程发出块  ReadKey方法,直到一个字符或功能键是pressed。

什么它实际上是获得对 Console.InternalSyncObject 一把锁,prevents进一步的操作控制台上。

What it actually does is acquire a lock on Console.InternalSyncObject, which prevents further operations on the console.

到Console.ReadLine()方法不会阻止这种方式螺纹

The Console.ReadLine() method does not block the thread in this way.

读this文章,我猜你有.NET 4.5安装?

Reading this article I'm guessing you have .NET 4.5 installed?

这篇关于为什么Console.WriteLine的Console.ReadKey()块输出调用另一个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:43