public FKNative() { m_OutBuffer = Marshal.AllocHGlobal((int)OUT_BUFFER_SIZE); m_InBuffer = Marshal.AllocHGlobal((int)IN_BUFFER_SIZE); m_DevIOCTL = new FKDeviceIOCTL(FKUsbTracerPath, false); m_TraceBufferThread = new Thread(Func_TraceBufReadThread); }
// 新的TraceReader线程 private void Func_TraceBufReadThread() { try { FKDeviceIOCTL devIO = new FKDeviceIOCTL(FKUsbTracerPath, true); while (true) { uint bytesReturned; // 向驱动发送 跟踪信息 的请求 bool result = devIO.DeviceIoControl(FKDeviceIOCTL.IOCTL_FKUSBTRACER_GET_BUFFER, IntPtr.Zero, 0, m_OutBuffer, OUT_BUFFER_SIZE, out bytesReturned); if (!result) { int err = Marshal.GetLastWin32Error(); // 检查I/O状态 if (err == ERROR_IO_PENDING) { while (true) { // 检查 I/O 请求是否被填充( 500ms 后要退出一次,不然线程死锁无法杀掉了 ) if (devIO.WaitForOverlappedIo(500, out bytesReturned)) { result = true; break; } } } else { System.Diagnostics.Debug.WriteLine(err); } } if (result) { // 收到数据了,我们回调通知 if (FilterTraceArrived != null) { FilterTraceArrived(this, new FKFilterTraceArrivedEventArgs(GetTraceList(m_OutBuffer, OUT_BUFFER_SIZE, bytesReturned))); } // 休眠,让出时间给主线程更新下UI Thread.Sleep(10); } } } catch (ThreadAbortException) { // 在本线程退出前,必须关闭当前pending状态的 IO m_DevIOCTL.CancelIo(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }