/// <summary> /// Reads or Writes data (depending on the <see cref="EpNum"/>) to/from the current <see cref="UsbEndpointReader"/>. /// </summary> /// <param name="buffer">The buffer for the tranfer.</param> /// <param name="offset">The position in buffer to start storing the data.</param> /// <param name="count">The number of bytes to send or the maximum number of bytes to receive.</param> /// <param name="timeout">Maximum time to wait for the transfer to complete. If the transfer times out, the IO operation will be cancelled.</param> /// <returns> /// Number of bytes transmitted or less than zero if an error occured. /// </returns> public int Transfer(byte[] buffer, int offset, int count, int timeout) { if (mbDisposed) { return((int)ErrorCodes.ENODEV); } if (!TransferLock.WaitOne(0, false)) { return((int)ErrorCodes.EBUSY); } int ret = -1; try { lock (oLockTransferContext) Context.Setup(buffer, offset, count, timeout, true); ret = transferSync(Context); } catch (Exception ex) { ret = (int)ErrorCodes.EEXCEPTION; UsbGlobals.Error(this, ex.ToString(), "Transfer", ret); } finally { freeAsync(Context); TransferLock.Release(); } return(ret); }
private void startStopReadThread() { if (IsDisposed) { return; } if (mDataReceivedEnabled) { mEventCancelReadThread.Set(); int iLoopCount = 0; while (mthReadThread.IsAlive) { iLoopCount++; if ((iLoopCount) > 1000) { mthReadThread.Abort(); UsbGlobals.Error(this, "Thread could not be gracefully stopped.", "startStopReadThread", (int)ErrorCodes.ETHREADABORT); break; } Application.DoEvents(); Thread.Sleep(1); if (Context.mContext.IsValid) { cancelAsync(Context); } } iLoopCount = 0; while (mDataReceivedEnabled) { iLoopCount++; Application.DoEvents(); Thread.Sleep(1); if ((iLoopCount) > 1000) { throw new LibUsbException(this, "startStopReadThread: The read thread is stalled."); } } mthReadThread = null; } else { if (!TransferLock.WaitOne(0, false)) { UsbGlobals.Error(this, "Read thread could not be started because a transfer is allready pending.", "startStopReadThread", (int)ErrorCodes.EBUSY); return; } mEventCancelReadThread.Reset(); lock (oLockTransferContext) Context.Setup(new byte[mReadBufferSize], 0, mReadBufferSize, Timeout.Infinite, false); mthReadThread = new Thread(ReadData); mthReadThread.Start(Context); Application.DoEvents(); } }