28 I/O限制的异步操作

28.2 C#的异步函数

        private static async Task<string> IssueClientRequestAsync(string serverName, string message)
        {
            using (var pipe = new NamedPipeClientStream(serverName, "PipeName", PipeDirection.InOut, PipeOptions.Asynchronous | PipeOptions.WriteThrough))
            {
                pipe.Connect();     //必须在ReadMode设置前连接
                pipe.ReadMode = PipeTransmissionMode.Message;

                byte[] request = Encoding.UTF8.GetBytes(message);
                await pipe.WriteAsync(request, 0, request.Length);

                byte[] response = new byte[10000];
                int bytesRead = await pipe.ReadAsync(response, 0, response.Length);
                return Encoding.UTF8.GetString(response, 0, bytesRead);
            }
        }

猜你喜欢

转载自www.cnblogs.com/kikyoqiang/p/10203197.html
28