当NamedPipeServer流从管道读取任何数据时,它不响应 CancellationTokenSource.Cancel()
这是为什么?
如何限制我在服务器中等待来自客户端的数据的时间?
代码重现:
static void Main(string[] args)
{
Server();
Clinet();
Console.WriteLine("press [enter] to exit");
Console.ReadLine();
}
private static async Task Server()
{
using (var cancellationTokenSource = new CancellationTokenSource(1000))
using (var server = new NamedPipeServerStream("test",
PipeDirection.InOut,
1,
PipeTransmissionMode.Byte,
PipeOptions.Asynchronous))
{
var cancellationToken = cancellationTokenSource.Token;
await server.WaitForConnectionAsync(cancellationToken);
await server.WriteAsync(new byte[]{1,2,3,4}, 0, 4, cancellationToken);
var buffer = new byte[4];
await server.ReadAsync(buffer, 0, 4, cancellationToken);
Console.WriteLine("exit server");
}
}
private static async Task Clinet()
{
using (var client = new NamedPipeClientStream(".", "test", …Run Code Online (Sandbox Code Playgroud) 在我的仓库中,我有以下情况:
$ git remote -v
origin http://repoA/_git/libs (fetch)
origin http://repoB/libs.git (push)
origin http://repoA/_git/qpp_libs (push)
Run Code Online (Sandbox Code Playgroud)
我也想从 获取repoB。这就是我想要实现这一目标的方式:
git remote set-url --add origin http://repoB/libs.git
Run Code Online (Sandbox Code Playgroud)
但运行上面的命令后,仍然只有 3 个 URL:
$ git remote -v
origin http://repoA/_git/libs (fetch)
origin http://repoB/libs.git (push)
origin http://repoA/_git/qpp_libs (push)
Run Code Online (Sandbox Code Playgroud)
如何添加另一个获取 URL?
试图理解流和async/await.如果我正确地await将执行返回给调用者,那么我希望下面代码的结果是:
before calling async ReadToEnd
after the call to ReadToEnd
before calling async ReadToEnd
after the call to ReadToEnd
Run Code Online (Sandbox Code Playgroud)
但它是这样的
before calling async ReadToEnd
after the call to ReadToEnd
before calling async ReadToEnd
after await in ReadToEnd. streamReader.BaseStream.GetType(): System.IO.MemoryStream
after the call to ReadToEnd
Run Code Online (Sandbox Code Playgroud)
似乎在调用时使用StreamReader带有FileStream下面的a 确实返回到调用者 StreamReader.ReadToEndAsync(),但是当使用StreamReader带有MemoryStream下面的时它不起作用.
试图了解发生了什么事我读过的.NET源代码,来到在调用conculssion StreamReader.ReadToEndAsync()与MemoryStream下最终调用ReadAsync()(来源上BaseStream).在的情况下MemoryStream的ReadAsync()仅仅是不是一个真正的asynhnornous方法,因此不会返回给调用者.
我的理解是否正确?
using System;
using System.Text;
using System.Linq; …Run Code Online (Sandbox Code Playgroud)