BeginWrite() public méthode

public BeginWrite ( byte buffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult
buffer byte
offset int
count int
callback AsyncCallback
state object
Résultat IAsyncResult
Exemple #1
0
    public void OnAsyncMessage(PipeStream pipe, Byte [] data, Int32 bytes, Object state)
    {
        Console.WriteLine("Message: " + (Int32) state + " bytes: " + bytes);
        data = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(data, 0, bytes).ToUpper().ToCharArray());

        // Write results
        try {
            pipe.BeginWrite(data, 0, bytes, OnAsyncWriteComplete, pipe);
        }
        catch (Exception) {
            pipe.Close();
        }
    }
		public static void Write(PipeStream stream, byte[] buffer, int offset, int count, bool isAsync, TimeoutHelper timeoutHelper)
		{
			// 异步时,使用异步方式写入数据
			if (isAsync)
			{
				IAsyncResult asyncResult = stream.BeginWrite(buffer, offset, count, null, null);

				// 等待 timeoutHelper 计算后的剩余时间,如果在这段时间内没有读到数据,那么将直接返回,这时,由于没有读到数据,返回的数据长度为 0
				asyncResult.AsyncWaitHandle.WaitOne(timeoutHelper == null ? 60000 : (int)timeoutHelper.RemainingTime().TotalMilliseconds);

				if (asyncResult.GetType() == pipeStreamAsyncResultType)
				{
					pipeStreamAsyncResult_waitHandle.SetValue(asyncResult, null);
				}

				stream.EndWrite(asyncResult);

				return;
			}

			// 使用系统内置的方式进行同步阻塞式读取,该方法直到读取到数据才返回
			stream.Write(buffer, offset, count);
		}