Example #1
0
        public void CloseInterface()
        {
            if ((holdErrorThreadOpen) || (holdDataThreadOpen))
            {
                return;
            }

            // Shut down event thread
            if (dataThreadActive)
            {
                dataThreadActive = false;
                FileIOApiDeclarations.SetEvent(readEvent);
                int n = 0;
                if (dataThreadHandle != null)
                {
                    while (dataThreadHandle.IsAlive)
                    {
                        Thread.Sleep(10);
                        n++;
                        if (n == 10)
                        {
                            dataThreadHandle.Abort(); break;
                        }
                    }
                    dataThreadHandle = null;
                }
            }

            // Shut down read thread
            if (readThreadActive)
            {
                readThreadActive = false;
                // Wait for thread to exit
                if (readThreadHandle != null)
                {
                    int n = 0;
                    while (readThreadHandle.IsAlive)
                    {
                        Thread.Sleep(10);
                        n++;
                        if (n == 10)
                        {
                            readThreadHandle.Abort(); break;
                        }
                    }
                    readThreadHandle = null;
                }
            }
            if (writeThreadActive)
            {
                writeThreadActive = false;
                FileIOApiDeclarations.SetEvent(writeEvent);
                if (writeThreadHandle != null)
                {
                    int n = 0;
                    while (writeThreadHandle.IsAlive)
                    {
                        Thread.Sleep(10);
                        n++;
                        if (n == 10)
                        {
                            writeThreadHandle.Abort(); break;
                        }
                    }
                    writeThreadHandle = null;
                }
            }
            if (errorThreadActive)
            {
                errorThreadActive = false;
                if (errorThreadHandle != null)
                {
                    int n = 0;
                    while (errorThreadHandle.IsAlive)
                    {
                        Thread.Sleep(10);
                        n++;
                        if (n == 10)
                        {
                            errorThreadHandle.Abort(); break;
                        }
                    }
                    errorThreadHandle = null;
                }
            }

            if (writeRing != null)
            {
                writeRing = null;
            }
            if (readRing != null)
            {
                readRing = null;
            }

            //  if (readEvent != null) {readEvent = null;}
            //  if (writeEvent != null) { writeEvent = null; }

            if ((0x00FF != pid && 0x00FE != pid && 0x00FD != pid && 0x00FC != pid && 0x00FB != pid) || version > 272)
            {
                // it's not an old VEC foot pedal (those hang when closing the handle)
                if (readFileHandle != null)     // 9/1/09 - readFileHandle != null ||added by Onur to avoid null reference exception
                {
                    if (!readFileHandle.IsInvalid)
                    {
                        readFileHandle.Close();
                    }
                }
                if (writeFileHandle != null)
                {
                    if (!writeFileHandle.IsInvalid)
                    {
                        writeFileHandle.Close();
                    }
                }
            }
            connected = false;
        }
Example #2
0
        }        //DataEventThread()

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

//-----------------------------------------------------------------------------
        /// <summary>
        /// Sets connection to the enumerated device.
        /// If inputReportSize greater than zero it generates a read handle.
        /// If outputReportSize greater than zero it generates a write handle.
        /// </summary>
        /// <returns></returns>
        public Int32 SetupInterface()
        {
            int retin  = 0;
            int retout = 0;

            if (connected)
            {
                return(203);
            }
            if (inputReportSize > 0)
            {
                readFileHandle = FileIOApiDeclarations.CreateFile(path, FileIOApiDeclarations.GENERIC_READ,
                                                                  FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE,
                                                                  IntPtr.Zero, FileIOApiDeclarations.OPEN_EXISTING, FileIOApiDeclarations.FILE_FLAG_OVERLAPPED, 0);

                if (readFileHandle.IsInvalid)
                {
                    //readEvent = null;
                    //readFileHandle = null;
                    readRing = null;
                    //CloseInterface();
                    retin = 207;
                    goto outputinit;
                }
                readEvent        = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, "");
                readRing         = new RngBuf2(128, inputReportSize);
                readThreadHandle = new Thread(new ThreadStart(ReadThread));
                readThreadHandle.IsBackground = true;
                readThreadHandle.Name         = "PIEHidReadThread for " + pid;
                readThreadActive = true;
                readThreadHandle.Start();
            }
outputinit:
            if (outputReportSize > 0)
            {
                writeFileHandle = FileIOApiDeclarations.CreateFile(path, FileIOApiDeclarations.GENERIC_WRITE,
                                                                   FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE,
                                                                   IntPtr.Zero, FileIOApiDeclarations.OPEN_EXISTING,
                                                                   FileIOApiDeclarations.FILE_FLAG_OVERLAPPED,
                                                                   0);

                if (writeFileHandle.IsInvalid)
                {
                    // writeEvent = null;
                    // writeFileHandle = null;
                    writeRing = null;
                    //CloseInterface();
                    retout = 208;
                    goto ErrorOut;
                }
                writeEvent        = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, "");
                writeRing         = new RngBuf2(128, outputReportSize);
                writeThreadHandle = new Thread(new ThreadStart(WriteThread));
                writeThreadHandle.IsBackground = true;
                writeThreadHandle.Name         = "PIEHidWriteThread for " + pid;
                writeThreadActive = true;
                writeThreadHandle.Start();
            }
            connected = true;
ErrorOut:
            if ((retin == 0) && (retout == 0))
            {
                return(0);
            }
            if ((retin == 207) && (retout == 208))
            {
                return(209);
            }
            else
            {
                return(retin + retout);
            }
        }