private void ThreadJob() { int lpNumberOfBytesRead = 0; while ((comDevice.ToInt32() > 0) && comThread.IsAlive) { try { int num2; COMSTAT comstat; ulong lineStatus = GetLineStatus(comDevice); if (m_LineStatus != lineStatus) { m_LineStatus = lineStatus; if (NeedSynchronization) { lock (synchronizeVariable) { OnLineStatusChanged(new LineStatusEventArgs(lineStatus)); } } else OnLineStatusChanged(new LineStatusEventArgs(lineStatus)); } comstat = new COMSTAT(); NativeMethods.ClearCommError(comDevice, out num2, ref comstat); int cbInQue = comstat.cbInQue; if ((cbInQue > 0 && comDevice.ToInt32() > 0) && comThread.IsAlive) { byte[] buffer = new byte[cbInQue]; m_IsReceiving = true; NativeMethods.ReadFile(comDevice, buffer, cbInQue, ref lpNumberOfBytesRead, 0); m_IsReceiving = false; if (lpNumberOfBytesRead > 0) { if (NeedSynchronization) { lock (synchronizeVariable) { OnReceived(new DataEventArgs(buffer)); } } else OnReceived(new DataEventArgs(buffer)); } } } catch { m_IsReceiving = false; } if (comDevice.ToInt32() > 0 && comThread.IsAlive) { Thread.Sleep(Delay); } } }
internal static extern int ClearCommError(IntPtr hFile, out int lpErrors, ref COMSTAT lpComStat);