public int EndRead(IAsyncResult itfAR)
        {
            if (itfAR == null)
            {
                throw new ArgumentNullException("AsyncResult cannot be null");
            }
            if (!(itfAR is CanAsyncResult))
            {
                throw new ArgumentException("AsyncResult object was not created by calling BeginRead on this class.");
            }

            CanAsyncResult result = (CanAsyncResult)itfAR;

            try
            {
                if (!result.IsCompleted)
                {
                    result.AsyncWaitHandle.WaitOne();
                }

                if (result.Error != null)
                {
                    throw new USBException("Asynchronous read from pipe has failed.", result.Error);
                }

                return(result.MessageTransfered);
            }
            finally
            {
                result.Dispose();
            }
        }
        /// <summary>
        /// Opens a new CAN bus connection.
        /// </summary>
        public void Open()
        {
            LasException = null;

            if (!IsConnected)
            {
                throw new CanAdapterException("Adapter is Disconnected. Code:-8604."); //Doc
            }
            if (!_isOpen)
            {
                IncomingMsgQueue = new SafeQueue <CanMessage>();
                OutgoingMsgQueue = new SafeQueue <CanMessage>();

                _isOpen = true;
                isAbort = false;
                _asyncReadBeginResult = null;

                Services.Start();

                if (Attributes.State != CanState.SDEV_START)
                {
                    throw new CanAdapterException("Unable to open the bus. Code:-8607."); //Doc
                }
                Usb.Pipes[USB_MSG_IN_ADDR].Flush();

                _asynReadCpltCallback = new AsyncCallback(MsgPipeReadComplate);
                _asyncReadResult      = Usb.Pipes[USB_MSG_IN_ADDR].BeginRead(rxMsgPacketBuffer, 0, UsbMsgInEpSize, _asynReadCpltCallback, Usb);
            }
            else
            {
                throw new CanAdapterException("The bus is already opened. Code:-8608."); //Doc
            }
        }
        public IAsyncResult BeginRead(CanMessage[] frames, int offset, int length, AsyncCallback userCallback, object stateObject)
        {
            if (!IsConnected)
            {
                throw new CanAdapterException("Adapter is Disconnected. Code:-8604."); //Doc
            }
            if (!_isOpen)
            {
                throw new CanAdapterException("Bus is Closed. Code:-8605"); //Doc
            }
            if (frames == null)
            {
                throw new ArgumentNullException("Frame Buffer cannot be null. Code:-8606."); //Doc
            }
            _readBeginFrameArray = frames;
            _readBeginLength     = length;
            _readBeginOffset     = offset;

            _asyncReadBeginResult = new CanAsyncResult(userCallback, stateObject);
            try
            {
                //Van annyi elem, hogy most azonnal vége legyen.
                if (IncomingMsgQueue.Count >= length)
                {
                    Read(_readBeginFrameArray, offset, length);
                    _asyncReadBeginResult.OnCompletion(true, null, length, true);
                }
            }
            catch (Exception)
            {
                _asyncReadBeginResult.Dispose();
                throw;
            }

            return(_asyncReadBeginResult);
        }