- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
使用以下代码启动一个新的控制台应用程序 -
class Program
{
static void Main(string[] args)
{
while (true)
{
Task<string> readLineTask = Console.In.ReadLineAsync();
Debug.WriteLine("hi");
}
}
}
Console.In.ReadLineAsync 处于阻塞状态,直到在控制台中输入一行后才会返回。因此“Hi”永远不会写入控制台。
在 Console.In.ReadLineAsync 上使用 await 也会阻塞。
据我了解,新的 Async CTP 方法不会阻塞。
这是什么原因?
还有一个例子
static void Main(string[] args)
{
Task delayTask = Task.Delay(50000);
Debug.WriteLine("hi");
}
这如我所料,它直接进入下一行并打印“hi”,因为 Task.Delay 不会阻塞。
最佳答案
daryal 在这里提供了答案 http://smellegantcode.wordpress.com/2012/08/28/a-boring-discovery/
看起来 ReadLineAsync 实际上并没有做它应该做的事情。框架中的错误。
我的解决方案是在循环中使用 ThreadPool.QueueUserWorkItem,这样每次对 ReadLineAsync 的调用都在新线程上完成。
关于c# - 为什么 Console.In.ReadLineAsync 会阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14724582/
使用以下代码启动一个新的控制台应用程序 - class Program { static void Main(string[] args) { while (true)
我正在尝试实现客户端-服务器通信,它应该支持以换行符结尾的命令。做这样的事情完全可以吗: public partial class MainWindow : Window { public M
当我通过调用 CancellationTokenSource 的 Cancel() 方法取消具有以下内容的异步方法时,它最终会停止。然而,由于 Console.WriteLine(await read
当我创建尝试读取这样的行时,Stream.Position 在第一次调用 StreamReader.ReadLineAsync 后更改一次,然后在多次调用后不会更改。 var stream = new
有谁知道为什么 ReadLineAsync 和 ReadToEndAsync 比同步对应的 ReadLine 和 ReadToEnd 慢得多?如果我正在等待多个调用,我可以理解这种缓慢,但事实并非如此
通过所有异步/等待(来自线程池),我遇到了一个有趣的挑战。 我有一个在 WPF 应用程序中运行的 TCP 服务器,该应用程序接受客户端并将它们存储在 List<> 中像这样: private List
我在一个项目中坚持使用 .NET 4.0。 StreamReader 不提供异步或开始/结束版本的 ReadLine。底层 Stream 对象具有 BeginRead/BeginEnd,但它们采用字节
使用 Blazor wasm 应用程序(NET 标准 2.1)和 BlazorFileReader在下面的代码片段中(缩短,在我的应用程序中,它被分为一个 .razor 文件和一个 .razor.cs
我是一名优秀的程序员,十分优秀!