当前位置: 首页>>代码示例>>C#>>正文


C# this.BeginRead方法代码示例

本文整理汇总了C#中this.BeginRead方法的典型用法代码示例。如果您正苦于以下问题:C# this.BeginRead方法的具体用法?C# this.BeginRead怎么用?C# this.BeginRead使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在this的用法示例。


在下文中一共展示了this.BeginRead方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AsyncCopy

        public static WaitHandle AsyncCopy(this Stream src, Stream dst)
        {
            var evt = new ManualResetEvent(false);
            var buffer = new byte[BUFFER_SIZE];

            AsyncCallback cbk = null;
            cbk = (asyncReadResult) =>
            {
                int bytesRead = src.EndRead(asyncReadResult);
                if (bytesRead > 0)
                {
                    //Console.Write("r");
                    dst.BeginWrite(buffer, 0, bytesRead,
                        (asyncWriteResult) =>
                        {
                            //Console.Write("w");
                            dst.EndWrite(asyncWriteResult);
                            src.BeginRead(buffer, 0, buffer.Length, cbk, null);
                        }, null);
                }
                else
                {
                    Console.WriteLine();
                    dst.Flush();
                    dst.Position = 0;
                    evt.Set();
                }
            };
            src.BeginRead(buffer, 0, buffer.Length, cbk, buffer);
            return evt;
        }
开发者ID:luismiguellopes,项目名称:Personal.CodeLibrary,代码行数:31,代码来源:StreamLib.cs

示例2: ReadBuffer

        public static Task<byte[]> ReadBuffer(this Stream stream)
        {
            var completionSource = new TaskCompletionSource<byte[]>();
            const int defaultBufferSize = 1024 * 16;
            var buffer = new byte[defaultBufferSize];
            var list = new List<byte>();
            AsyncCallback callback = null;
            callback = ar =>
            {
                int read;
                try
                {
                    read = stream.EndRead(ar);
                }
                catch (Exception e)
                {
                    completionSource.SetException(e);
                    return;
                }
                list.AddRange(buffer.Take(read));
                if (read == 0)
                {
                    completionSource.SetResult(list.ToArray());
                    return;
                }
                stream.BeginRead(buffer, 0, buffer.Length, callback, null);
            };
            stream.BeginRead(buffer, 0, buffer.Length, callback, null);

            return completionSource.Task;
        }
开发者ID:nieve,项目名称:ravenmq,代码行数:31,代码来源:StreamExtension.cs

示例3: CopyTo

        // http://stackoverflow.com/questions/1540658/net-asynchronous-stream-read-write
        public static void CopyTo(this Stream input, Stream output, int bufferSize)
        {
            if (!input.CanRead) throw new InvalidOperationException("input must be open for reading");
            if (!output.CanWrite) throw new InvalidOperationException("output must be open for writing");

            byte[][] buf = { new byte[bufferSize], new byte[bufferSize] };
            int[] bufl = { 0, 0 };
            int bufno = 0;

            IAsyncResult read = input.BeginRead(buf[bufno], 0, buf[bufno].Length, null, null);
            IAsyncResult write = null;

            while (true) {

                // wait for the read operation to complete
                read.AsyncWaitHandle.WaitOne();
                bufl[bufno] = input.EndRead(read);

                // if zero bytes read, the copy is complete
                if (bufl[bufno] == 0) {
                    break;
                }

                // wait for the in-flight write operation, if one exists, to complete
                // the only time one won't exist is after the very first read operation completes
                if (write != null) {
                    write.AsyncWaitHandle.WaitOne();
                    output.EndWrite(write);
                }

                // start the new write operation
                write = output.BeginWrite(buf[bufno], 0, bufl[bufno], null, null);

                // toggle the current, in-use buffer
                // and start the read operation on the new buffer
                bufno = (bufno == 0 ? 1 : 0);
                read = input.BeginRead(buf[bufno], 0, buf[bufno].Length, null, null);

            }

            // wait for the final in-flight write operation, if one exists, to complete
            // the only time one won't exist is if the input stream is empty.
            if (write != null) {
                write.AsyncWaitHandle.WaitOne();
                output.EndWrite(write);
            }

            output.Flush();

            // return to the caller ;
            return;
        }
开发者ID:cynosura,项目名称:dotless,代码行数:53,代码来源:System.IO.Stream.Extensions.cs

示例4: CopyTo

        /// <summary>
        /// Copies the contents between two <see cref="Stream"/> instances in an async fashion.
        /// </summary>
        /// <param name="source">The source stream to copy from.</param>
        /// <param name="destination">The destination stream to copy to.</param>
        /// <param name="onComplete">Delegate that should be invoked when the operation has completed. Will pass the source, destination and exception (if one was thrown) to the function. Can pass in <see langword="null" />.</param>
        public static void CopyTo(this Stream source, Stream destination, Action<Stream, Stream, Exception> onComplete)
        {
            var buffer =
                new byte[BufferSize];

            Action<Exception> done = e =>
            {
                if (onComplete != null)
                {
                    onComplete.Invoke(source, destination, e);
                }
            };

            AsyncCallback rc = null;

            rc = readResult =>
            {
                try
                {
                    var read =
                        source.EndRead(readResult);

                    if (read <= 0)
                    {
                        done.Invoke(null);
                        return;
                    }

                    destination.BeginWrite(buffer, 0, read, writeResult =>
                    {
                        try
                        {
                            destination.EndWrite(writeResult);
                            source.BeginRead(buffer, 0, buffer.Length, rc, null);
                        }
                        catch (Exception ex)
                        {
                            done.Invoke(ex);
                        }

                    }, null);
                }
                catch (Exception ex)
                {
                    done.Invoke(ex);
                }
            };

            source.BeginRead(buffer, 0, buffer.Length, rc, null);
        }
开发者ID:JulianRooze,项目名称:Nancy,代码行数:56,代码来源:StreamExtensions.cs

示例5: QueueRead

 public static void QueueRead( this Stream stream, Func<ArraySegment<byte>, Action, bool> callback, Action<Exception> error, Action complete )
 {
     var buffer = new byte[4*1024];
     stream.BeginRead(
             buffer,
             0,
             buffer.Length,
             x =>
                 {
                     var read = 0;
                     try
                     {
                         read = stream.EndRead( x );
                     }
                     catch (Exception e)
                     {
                         error( e );
                     }
                     if( read > 0 )
                     {
                         var waitForCall = callback( new ArraySegment<byte>(buffer, 0, read), () => QueueRead( stream, callback, error, complete ) );
                         if( !waitForCall )
                             QueueRead( stream, callback, error, complete );
                     }
                     else
                     {
                         complete();
                     }
                 },
             null
         );
 }
开发者ID:code-attic,项目名称:hyperstack,代码行数:32,代码来源:OwinStreamExtension.cs

示例6: ReadToEndAsync

        public static K<string> ReadToEndAsync(this Stream stream)
        {
            return respond =>
                {
                    StringBuilder sb = new StringBuilder();
                    byte[] buffer = new byte[1024];

                    Func<IAsyncResult> readChunk = null;

                    readChunk = () => stream.BeginRead(buffer, 0, 1024, result =>
                        {
                            int read = stream.EndRead(result);
                            if (read > 0)
                            {
                                sb.Append(Encoding.UTF8.GetString(buffer, 0, read));

                                readChunk();
                            }
                            else
                            {
                                stream.Dispose();

                                respond(sb.ToString());
                            }
                        }, null);
                    readChunk();
                };
        }
开发者ID:KevM,项目名称:Magnum,代码行数:28,代码来源:ExtensionsForWebStuff.cs

示例7: ReadAsync

        /// <summary>
        /// 지정된 스트림을 비동기 방식으로 읽어, <paramref name="buffer"/>에 채웁니다. 작업의 결과는 읽은 바이트 수입니다.
        /// </summary>
        public static Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count) {
            if(IsDebugEnabled)
                log.Debug("비동기 방식으로 스트림을 읽어 버퍼에 씁니다... offset=[{0}], count=[{1}]", offset, count);

            // Silverlight용 TPL에서는 지원하지 3개 이상의 인자를 지원하지 않는다.
            var ar = stream.BeginRead(buffer, offset, count, null, null);
            return Task.Factory.StartNew(() => stream.EndRead(ar));
        }
开发者ID:debop,项目名称:NFramework,代码行数:11,代码来源:StreamAsync.cs

示例8: BeginRead

        /// <summary>
        /// Extends BeginRead so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// isolatedstoragefilestream.BeginRead(buffer, userCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginRead(this IsolatedStorageFileStream isolatedstoragefilestream, Byte[] buffer, AsyncCallback userCallback)
        {
            if(isolatedstoragefilestream == null) throw new ArgumentNullException("isolatedstoragefilestream");

            if(buffer == null) throw new ArgumentNullException("buffer");

            return isolatedstoragefilestream.BeginRead(buffer, 0, buffer.Length, userCallback);
        }
开发者ID:peteraritchie,项目名称:ProductivityExtensions,代码行数:14,代码来源:IsolatedStorageFileStreamable.g.cs

示例9: BeginReadToEnd

 public static IAsyncResult BeginReadToEnd(this Stream stream, byte[] buffer, int offset, int count, AsyncCallback callback, Object state)
 {
     byte[] tempBuffer = new byte[count];
     ByteArrayAsyncResult result = new ByteArrayAsyncResult(callback, state, buffer, offset, tempBuffer);
     ByteArrayAsyncState asyncState = new ByteArrayAsyncState {Result = result, Stream = stream};
     stream.BeginRead(tempBuffer, 0, count, OnRead, asyncState);
     return result;
 }
开发者ID:peteraritchie,项目名称:dotNetEvolution,代码行数:8,代码来源:StreamExtensions.cs

示例10: ReadBytes

 public static IObservable<byte[]> ReadBytes(this Stream stream, int count)
 {
     var buffer = new byte[count];
     return Observable.FromAsyncPattern((cb, state) => stream.BeginRead(buffer, 0, count, cb, state), ar => {
         stream.EndRead(ar);
         return buffer;
     })();
 }
开发者ID:skovsboll,项目名称:PicoBus,代码行数:8,代码来源:StreamExtensions.cs

示例11: BeginRead

        /// <summary>
        /// Extends BeginRead so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// pipestream.BeginRead(buffer, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginRead(this NamedPipeServerStream pipestream, Byte[] buffer, AsyncCallback callback)
        {
            if(pipestream == null) throw new ArgumentNullException("pipestream");

            if(buffer == null) throw new ArgumentNullException("buffer");

            return pipestream.BeginRead(buffer, 0, buffer.Length, callback);
        }
开发者ID:peteraritchie,项目名称:ProductivityExtensions,代码行数:14,代码来源:NamedPipeServerStreamable.g.cs

示例12: BeginRead

        /// <summary>
        /// Extends BeginRead so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// filestream.BeginRead(array, userCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginRead(this FileStream filestream, Byte[] array, AsyncCallback userCallback)
        {
            if(filestream == null) throw new ArgumentNullException("filestream");

            if(array == null) throw new ArgumentNullException("array");

            return filestream.BeginRead(array, 0, array.Length, userCallback);
        }
开发者ID:peteraritchie,项目名称:ProductivityExtensions,代码行数:14,代码来源:FileStreamable.g.cs

示例13: BeginRead

        /// <summary>
        /// Extends BeginRead so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// gzipstream.BeginRead(array, asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginRead(this GZipStream gzipstream, Byte[] array, AsyncCallback asyncCallback)
        {
            if(gzipstream == null) throw new ArgumentNullException("gzipstream");

            if(array == null) throw new ArgumentNullException("array");

            return gzipstream.BeginRead(array, 0, array.Length, asyncCallback);
        }
开发者ID:peteraritchie,项目名称:ProductivityExtensions,代码行数:14,代码来源:GZipStreamable.g.cs

示例14: BeginRead

        /// <summary>
        /// Extends BeginRead so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// stream.BeginRead(buffer, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginRead(this Stream stream, Byte[] buffer, AsyncCallback callback)
        {
            if(stream == null) throw new ArgumentNullException("stream");

            if(buffer == null) throw new ArgumentNullException("buffer");

            return stream.BeginRead(buffer, 0, buffer.Length, callback);
        }
开发者ID:peteraritchie,项目名称:ProductivityExtensions,代码行数:14,代码来源:Streamable.g.cs

示例15: BeginRead

        /// <summary>
        /// Extends BeginRead so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// deflatestream.BeginRead(array, asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginRead(this DeflateStream deflatestream, Byte[] array, AsyncCallback asyncCallback)
        {
            if(deflatestream == null) throw new ArgumentNullException("deflatestream");

            if(array == null) throw new ArgumentNullException("array");

            return deflatestream.BeginRead(array, 0, array.Length, asyncCallback);
        }
开发者ID:peteraritchie,项目名称:ProductivityExtensions,代码行数:14,代码来源:DeflateStreamable.g.cs


注:本文中的this.BeginRead方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。