public void Dispose()
 {
     this.Close();
     NativeFunctions.candle_dev_free(this.FDeviceHandle);
 }
        void ThreadedUpdate()
        {
            while (!this.FIsClosing)
            {
                try
                {
                    // Rx frames
                    {
                        NativeFunctions.candle_frame_t nativeFrame;
                        if (NativeFunctions.candle_frame_read(this.FDeviceHandle
                                                              , out nativeFrame
                                                              , 0))
                        {
                            // Find the channel
                            if (this.FChannels.ContainsKey(nativeFrame.channel))
                            {
                                var frame = new Frame();

                                var flags = (NativeFunctions.candle_id_flags)(nativeFrame.can_id);
                                frame.Identifier = nativeFrame.can_id & ((1 << 29) - 1);
                                frame.Extended   = flags.HasFlag(NativeFunctions.candle_id_flags.CANDLE_ID_EXTENDED);
                                frame.RTR        = flags.HasFlag(NativeFunctions.candle_id_flags.CANDLE_ID_RTR);
                                frame.Error      = flags.HasFlag(NativeFunctions.candle_id_flags.CANDLE_ID_ERR);

                                frame.Data = new byte[nativeFrame.can_dlc];
                                Buffer.BlockCopy(nativeFrame.data, 0, frame.Data, 0, nativeFrame.can_dlc);

                                frame.Timestamp = nativeFrame.timestamp_us;

                                this.FChannels[nativeFrame.channel].NotifyReceive(frame);
                            }
                            else
                            {
                                throw (new Exception(String.Format("Cannot find channel {0}", nativeFrame.channel)));
                            }
                        }
                    }

                    // Perform actions
                    {
                        int count = 0;

                        Action action;
                        while (this.FActionQueue.TryTake(out action))
                        {
                            action();

                            if (count++ > 64)
                            {
                                // We have trouble when sending more than 92 message in a row
                                break;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.Write(e);
                }
            }
        }