本文整理汇总了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;
}
示例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;
}
示例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;
}
示例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);
}
示例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
);
}
示例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();
};
}
示例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));
}
示例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);
}
示例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;
}
示例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;
})();
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}