zoukankan      html  css  js  c++  java
  • 异步函数Demo

     1         private static async Task<String> IssueClientRequestAsync(string serverName, string message)
     2         {
     3             Console.WriteLine("进入IssueClientRequestAsync");
     4             //NamedPipeClientStream 公开 System.IO.Stream 周围命名管道,支持同步和异步读取和写入操作。
     5             //新实例初始化 System.IO.Pipes.NamedPipeClientStream 使用指定的管道和服务器名称,以及指定的管道方向和管道选项的类。
     6             // 参数:
     7             //   serverName:
     8             //     要连接的远程计算机的名称,或者为“.”,以指定本地计算机。
     9             //
    10             //   pipeName:
    11             //     管道的名称。
    12             //
    13             //   direction:
    14             //     确定管道方向的枚举值之一。(此处指定的管道方向是双向的)
    15             //
    16             //   options:
    17             //     确定如何打开或创建管道的枚举值之一。
    18             using (var pipe = new NamedPipeClientStream(serverName, "PipeName", PipeDirection.InOut, PipeOptions.Asynchronous | PipeOptions.WriteThrough))
    19             {
    20                 Console.WriteLine("Connect");
    21                 //连接到具有无限超时值的等待的服务器。
    22                 //必须在设置ReadMode之前连接
    23                 pipe.Connect();
    24 
    25                 Console.WriteLine("ReadMode");
    26                 //对象的读取模式
    27                 //指示是发送和读取的 消息流 的形式在管道中的数据,另外还有字节流(Byte)
    28                 pipe.ReadMode = PipeTransmissionMode.Message;
    29 
    30                 Console.WriteLine("request");
    31                 byte[] request = Encoding.UTF8.GetBytes(message);
    32 
    33                 Console.WriteLine("WriteAsync");
    34                 //将字节序列异步写入当前流,并将流的当前位置提升写入的字节数。
    35                 //   buffer:
    36                 //     从中写入数据的缓冲区。
    37                 //
    38                 //   offset:
    39                 //     buffer 中的从零开始的字节偏移量,从此处开始将字节复制到该流。
    40                 //
    41                 //   count:
    42                 //     最多写入的字节数。
    43                 await pipe.WriteAsync(request, 0, request.Length);
    44 
    45                 Console.WriteLine("response");
    46                 byte[] response = new byte[1000];
    47 
    48                 Console.WriteLine("ReadAsync");
    49                 //从当前流异步读取字节序列,并将流中的位置提升读取的字节数。
    50                 //   buffer:
    51                 //     数据写入的缓冲区。
    52                 //
    53                 //   offset:
    54                 //     buffer 中的字节偏移量,从该偏移量开始写入从流中读取的数据。
    55                 //
    56                 //   count:
    57                 //     最多读取的字节数。
    58                 int bytesRead = await pipe.ReadAsync(response, 0, response.Length);
    59 
    60                 Console.WriteLine("retuen");
    61                 return Encoding.UTF8.GetString(response, 0, bytesRead);
    62             }
    63         }
    64 
    65         private static void NamedPipeClientStreamTest()
    66         {
    67             using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "testpipe", PipeDirection.In))
    68             {
    69 
    70                 // Connect to the pipe or wait until the pipe is available.
    71                 Console.Write("Attempting to connect to pipe...");
    72                 pipeClient.Connect();
    73 
    74                 Console.WriteLine("Connected to pipe.");
    75                 Console.WriteLine("There are currently {0} pipe server instances open.", pipeClient.NumberOfServerInstances);
    76                 using (StreamReader sr = new StreamReader(pipeClient))
    77                 {
    78                     // Display the read text to the console
    79                     string temp;
    80                     while ((temp = sr.ReadLine()) != null)
    81                     {
    82                         Console.WriteLine("Received from server: {0}", temp);
    83                     }
    84                 }
    85             }
    86         }
  • 相关阅读:
    hdu5926Mr. Frog’s Game
    hdu5926Mr. Frog’s Game
    hdu5924Mr. Frog’s Problem
    hdu5924Mr. Frog’s Problem
    hdu5922Minimum’s Revenge
    hdu5922Minimum’s Revenge
    带空格的字符串输入
    带空格的字符串输入
    382. Linked List Random Node
    319. Bulb Switcher
  • 原文地址:https://www.cnblogs.com/xuejietong/p/8970886.html
Copyright © 2011-2022 走看看