Пример #1
0
        /// <summary>
        /// 异步写
        /// </summary>
        /// <param name="p_stream"></param>
        /// <param name="p_buffer"></param>
        /// <param name="p_offset"></param>
        /// <param name="p_count"></param>
        /// <param name="p_cancellationToken"></param>
        /// <returns></returns>
        public static Task WriteAsync(this Stream p_stream, byte[] p_buffer, int p_offset, int p_count, CancellationToken p_cancellationToken)
        {
            if (p_cancellationToken.IsCancellationRequested)
            {
                TaskCompletionSource <object> source = new TaskCompletionSource <object>();
                source.SetCanceled();
                return(source.Task);
            }
            TaskFactory factory = Task.Factory;
            Stream      stream2 = p_stream;
            Stream      stream3 = p_stream;

            return(factory.FromAsync <byte[], int, int>(new System.Func <byte[], int, int, AsyncCallback, object, IAsyncResult>(stream2.BeginWrite), new Action <IAsyncResult>(stream3.EndWrite), p_buffer, p_offset, p_count, null));
        }