BeginRead() public method

public BeginRead ( byte buffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult
buffer byte
offset int
count int
callback AsyncCallback
state object
return IAsyncResult
Ejemplo n.º 1
0
		public static int Read(PipeStream stream, byte[] buffer, int offset, int count, bool isAsync, TimeoutHelper timeoutHelper)
		{
			// 异步时,使用异步方式读取数据
			if (isAsync)
			{
				IAsyncResult asyncResult = stream.BeginRead(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);
				}

				return stream.EndRead(asyncResult);
			}

			// 使用系统内置的方式进行同步阻塞式读取,该方法直到读取到数据才返回
			return stream.Read(buffer, offset, count);
		}
        protected override int Read(byte[] buffer, int offset, int count)
        {
            pipe = (PipeStream)Stream;
            AsyncCallback cb = delegate (IAsyncResult ar)
            {
                //wHandle = ar.AsyncWaitHandle;

                pipe.EndRead (ar);
                ((ManualResetEvent)wHandle).Set ();

            };

            //((AutoResetEvent)wHandle).
            IAsyncResult iar = pipe.BeginRead (buffer, offset, count, cb, wHandle);

            Thread.Sleep (100);
            wHandle.WaitOne ();
            ((ManualResetEvent)wHandle).Reset ();

            return count;
        }
Ejemplo n.º 3
0
        public void ReadAsync(PipeStream stream, int timeout, Action<byte[]> onComplete)
        {
            var memoryStream = new MemoryStream();
            var buffer = new byte[8 * 1024];
            var read = 0;
            if (stream.CanTimeout)
                stream.ReadTimeout = timeout;

            Future.Of(
                x => stream.BeginRead(buffer, 0, buffer.Length, x, null),
                x =>
                {
                    read = stream.EndRead(x);
                    if (read > 0)
                    {
                        memoryStream.Write(buffer, 0, read);
                    }

                    if (stream.IsMessageComplete && memoryStream.Length > 0)
                    {
                        onComplete(memoryStream.ToArray());
                        memoryStream.Close();
                        memoryStream.Dispose();
                        memoryStream = new MemoryStream();
                    }
                    return read;
                })
                .LoopWhile(() => Running)
                .Start();
        }