BeginWrite(byte [] buffer, int offset, int count, AsyncCallback callback, object state) { if (!CanWrite) { throw new NotSupportedException("This stream does not support writing"); } // Creating a class derived from Stream that doesn't override BeginWrite // shows that it actually calls Write and does everything synchronously except // when invoking the callback, which is done from the ThreadPool. // Just put this in the Write override: // Console.WriteLine ("Write"); // Console.WriteLine (Environment.StackTrace); // Thread.Sleep (10000); StreamAsyncResult result = new StreamAsyncResult(state); try { Write(buffer, offset, count); result.SetComplete(null); } catch (Exception e) { result.SetComplete(e); } if (callback != null) { callback.BeginInvoke(result, null, null); } return(result); }
BeginRead(byte [] buffer, int offset, int count, AsyncCallback callback, object state) { if (!CanRead) { throw new NotSupportedException("This stream does not support reading"); } // Creating a class derived from Stream that doesn't override BeginRead // shows that it actually calls Read and does everything synchronously. // Just put this in the Read override: // Console.WriteLine ("Read"); // Console.WriteLine (Environment.StackTrace); // Thread.Sleep (10000); // return 10; StreamAsyncResult result = new StreamAsyncResult(state); try { int nbytes = Read(buffer, offset, count); result.SetComplete(null, nbytes); } catch (Exception e) { result.SetComplete(e, 0); } if (callback != null) { callback(result); } return(result); }
/// <summary>Begins an asynchronous write operation.</summary> /// <returns>An IAsyncResult that represents the asynchronous write, which could still be pending.</returns> /// <param name="buffer">The buffer to write data from. </param> /// <param name="offset">The byte offset in <paramref name="buffer" /> from which to begin writing. </param> /// <param name="count">The maximum number of bytes to write. </param> /// <param name="callback">An optional asynchronous callback, to be called when the write is complete. </param> /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests. </param> /// <exception cref="T:System.IO.IOException">Attempted an asynchronous write past the end of the stream, or a disk error occurs. </exception> /// <exception cref="T:System.ArgumentException">One or more of the arguments is invalid. </exception> /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> /// <exception cref="T:System.NotSupportedException">The current Stream implementation does not support the write operation. </exception> /// <filterpriority>2</filterpriority> public virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { if (!this.CanWrite) { throw new NotSupportedException("This stream does not support writing"); } StreamAsyncResult streamAsyncResult = new StreamAsyncResult(state); try { this.Write(buffer, offset, count); streamAsyncResult.SetComplete(null); } catch (Exception complete) { streamAsyncResult.SetComplete(complete); } if (callback != null) { callback.BeginInvoke(streamAsyncResult, null, null); } return(streamAsyncResult); }
/// <summary>Begins an asynchronous read operation.</summary> /// <returns>An <see cref="T:System.IAsyncResult" /> that represents the asynchronous read, which could still be pending.</returns> /// <param name="buffer">The buffer to read the data into. </param> /// <param name="offset">The byte offset in <paramref name="buffer" /> at which to begin writing data read from the stream. </param> /// <param name="count">The maximum number of bytes to read. </param> /// <param name="callback">An optional asynchronous callback, to be called when the read is complete. </param> /// <param name="state">A user-provided object that distinguishes this particular asynchronous read request from other requests. </param> /// <exception cref="T:System.IO.IOException">Attempted an asynchronous read past the end of the stream, or a disk error occurs. </exception> /// <exception cref="T:System.ArgumentException">One or more of the arguments is invalid. </exception> /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> /// <exception cref="T:System.NotSupportedException">The current Stream implementation does not support the read operation. </exception> /// <filterpriority>2</filterpriority> public virtual IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { if (!this.CanRead) { throw new NotSupportedException("This stream does not support reading"); } StreamAsyncResult streamAsyncResult = new StreamAsyncResult(state); try { int nbytes = this.Read(buffer, offset, count); streamAsyncResult.SetComplete(null, nbytes); } catch (Exception e) { streamAsyncResult.SetComplete(e, 0); } if (callback != null) { callback(streamAsyncResult); } return(streamAsyncResult); }
BeginWrite (byte [] buffer, int offset, int count, AsyncCallback callback, object state) { if (!CanWrite) throw new NotSupportedException ("This stream does not support writing"); // Creating a class derived from Stream that doesn't override BeginWrite // shows that it actually calls Write and does everything synchronously except // when invoking the callback, which is done from the ThreadPool. // Just put this in the Write override: // Console.WriteLine ("Write"); // Console.WriteLine (Environment.StackTrace); // Thread.Sleep (10000); StreamAsyncResult result = new StreamAsyncResult (state); try { Write (buffer, offset, count); result.SetComplete (null); } catch (Exception e) { result.SetComplete (e); } if (callback != null) callback.BeginInvoke (result, null, null); return result; }
BeginRead (byte [] buffer, int offset, int count, AsyncCallback callback, object state) { if (!CanRead) throw new NotSupportedException ("This stream does not support reading"); // Creating a class derived from Stream that doesn't override BeginRead // shows that it actually calls Read and does everything synchronously. // Just put this in the Read override: // Console.WriteLine ("Read"); // Console.WriteLine (Environment.StackTrace); // Thread.Sleep (10000); // return 10; StreamAsyncResult result = new StreamAsyncResult (state); try { int nbytes = Read (buffer, offset, count); result.SetComplete (null, nbytes); } catch (Exception e) { result.SetComplete (e, 0); } if (callback != null) callback (result); return result; }