//------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------
        protected void DataEventThread()
        {
            Byte[] currBuff = new Byte[inputReportSize];


            while (dataThreadActive)
            {
                if (readRing == null)
                {
                    return;
                }
                if (callNever == false)
                {
                    if (errCodeR != 0)
                    {
                        Array.Clear(currBuff, 0, inputReportSize);
                        holdDataThreadOpen = true;
                        registeredDataHandler.HandlePIEHidData(currBuff, this, errCodeR);
                        holdDataThreadOpen = false;
                        dataThreadActive   = false;
                    }
                    else if (readRing.get(currBuff) == 0)
                    {
                        holdDataThreadOpen = true;
                        registeredDataHandler.HandlePIEHidData(currBuff, this, 0);
                        holdDataThreadOpen = false;
                    }
                    if (readRing.IsEmpty())
                    {
                        FileIOApiDeclarations.ResetEvent(readEvent);
                    }
                }
                // System.Threading.Thread.Sleep(10);
                FileIOApiDeclarations.WaitForSingleObject(readEvent, 100);
            }    //while
            return;
        }        //DataEventThread()
//-------------------------------------------------------------------------------------------
        /// <summary>
        /// Write Thread
        /// </summary>
        protected void WriteThread()
        {
            //   FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttrUnused = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();
            IntPtr overlapEvent = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, "");

            FileIOApiDeclarations.OVERLAPPED overlapped = new FileIOApiDeclarations.OVERLAPPED();

            overlapped.Offset       = 0; // IntPtr.Zero;
            overlapped.OffsetHigh   = 0; // IntPtr.Zero;
            overlapped.hEvent       = overlapEvent;
            overlapped.Internal     = IntPtr.Zero;
            overlapped.InternalHigh = IntPtr.Zero;
            if (outputReportSize == 0)
            {
                return;
            }

            byte[]   buffer = new byte[outputReportSize];
            GCHandle wgch   = GCHandle.Alloc(buffer, GCHandleType.Pinned); //onur March 2009 - pinning is reuired

            int byteCount = 0;;

            //  int loopCount = 0;

            errCodeW  = 0;
            errCodeWE = 0;
            while (writeThreadActive)
            {
                if (writeRing == null)
                {
                    errCodeW = 407; errCodeWE = 407; goto Error;
                }
                while (writeRing.get((byte[])buffer) == 0)
                {
                    if (0 == FileIOApiDeclarations.WriteFile(
                            writeFileHandle,
                            wgch.AddrOfPinnedObject(),
                            outputReportSize,
                            ref byteCount,
                            ref overlapped))
                    {
                        int result = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                        if (result != FileIOApiDeclarations.ERROR_IO_PENDING)
                        //if ((result == FileIOApiDeclarations.ERROR_INVALID_HANDLE) ||
                        //    (result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED))
                        {
                            if (result == 87)
                            {
                                errCodeW = 412; errCodeWE = 412;
                            }
                            else
                            {
                                errCodeW = result; errCodeWE = 408;
                            }
                            goto Error;
                        }   //if (result ==
                        else
                        {
                            // loopCount = 0;
                            // while (writeThreadActive)
                            // {

                            result = FileIOApiDeclarations.WaitForSingleObject(overlapEvent, 1000);
                            if (result == FileIOApiDeclarations.WAIT_OBJECT_0)
                            {
                                // errCodeWE=1400+loopCount;
                                goto WriteCompleted;
                            }
                            //  loopCount++;
                            //  if (loopCount > 10)
                            // {
                            errCodeW  = 411;
                            errCodeWE = 411;

                            goto Error;

                            //  }

                            /*    if (0 == FileIOApiDeclarations.GetOverlappedResult(readFileHandle, ref overlapped, ref byteCount, 0))
                             *  {
                             *      result = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                             *      // if (result == ERROR_INVALID_HANDLE || result == ERROR_DEVICE_NOT_CONNECTED)
                             *      if (result != FileIOApiDeclarations.ERROR_IO_INCOMPLETE)
                             *      {
                             *          errCodeW = 409;
                             *          errCodeWE = 409;
                             *
                             *          goto Error;
                             *      }//if(result == ERROR_INVALID_HANDLE
                             *  }//if(!GetOverlappedResult*/
                            //  }//while
                            //   goto Error;
                        }   //else if(result==ERROR_IO_PENDING){
                            // continue;
                    }
                    else
                    {
                        if ((long)byteCount != outputReportSize)
                        {
                            errCodeW  = 410;
                            errCodeWE = 410;
                        }
                        //iface->errCodeWE=1399;
                    }
                    WriteCompleted :;
                }   //while(get==
                FileIOApiDeclarations.WaitForSingleObject(writeEvent, 100);
                FileIOApiDeclarations.ResetEvent(writeEvent);
                // System.Threading.Thread.Sleep(100);
            }
Error:
            wgch.Free(); //onur

            return;
        }