/// <summary> /// Begins an asynchronous write operation. /// </summary> /// <param name="buffer">The buffer to write data from.</param> /// <param name="offset">The byte offset in 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> /// <returns>An IAsyncResult that represents the asynchronous write, which could still be pending.</returns> public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { WriteCheck(buffer, offset, count); if (count == 0 || m_SerialPort.SerialPortIo.WaitForWriteEvent(count, m_WriteTimeout)) { LocalAsync ar = new LocalAsync(state); if (count > 0) { InternalWrite(buffer, offset, count); } ar.IsCompleted = true; ar.CompletedSynchronously = true; if (callback != null) callback(ar); return ar; } else { WriteDelegate write = this.InternalBlockingWrite; return write.BeginInvoke(buffer, offset, count, callback, state); } }
/// <summary> /// Begins an asynchronous read operation. /// </summary> /// <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> /// <returns>An <see cref="System.IAsyncResult"/> object to be used with <see cref="EndRead"/>.</returns> public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { ReadCheck(buffer, offset, count); if (count == 0 || m_SerialPort.SerialPortIo.WaitForReadEvent(0)) { // Data in the buffer, we can return immediately LocalAsync<int> ar = new LocalAsync<int>(state); if (count > 0) { ar.Result = InternalRead(buffer, offset, count); } else { ar.Result = 0; } ar.IsCompleted = true; ar.CompletedSynchronously = true; if (callback != null) callback(ar); return ar; } else { // No data in buffer, so we create a thread in the background ReadDelegate read = this.InternalBlockingRead; return read.BeginInvoke(buffer, offset, count, callback, state); } }