이것은 SerialPort 클래스 내부에서 이미 연동되어 있습니다. 이벤트 처리기가 실행중인 경우 DataReceived 이벤트가 다시 실행되지 않도록하는 내부 잠금이 있습니다.
당신은 내가 여기를 재현하는 것은 Reference Source에 관련 코드를 볼 수 있습니다
private void CatchReceivedEvents(object src, SerialDataReceivedEventArgs e)
{
SerialDataReceivedEventHandler eventHandler = DataReceived;
SerialStream stream = internalSerialStream;
if ((eventHandler != null) && (stream != null)){
lock (stream) {
// SerialStream might be closed between the time the event runner
// pumped this event and the time the threadpool thread end up
// invoking this event handler. The above lock and IsOpen check
// ensures that we raise the event only when the port is open
bool raiseEvent = false;
try {
raiseEvent = stream.IsOpen && (SerialData.Eof == e.EventType || BytesToRead >= receivedBytesThreshold);
}
catch {
// Ignore and continue. SerialPort might have been closed already!
}
finally {
if (raiseEvent)
eventHandler(this, e); // here, do your reading, etc.
}
}
}
}
참고이 코드에서 lock (stream)
문. 또한 DataReceived 이벤트 핸들러는 무언가가 수신되지 않으면 호출되지 않는다는 것을 알 수 있습니다. 그리고 SerialData.Eof 이벤트 유형을주의해야합니다.이 이벤트 유형은 상당한 수의 프로그래머를 트집했습니다.
도움이 필요하지 않습니다.
잠금 장치를 사용할 수 있습니까? – i3arnon
포럼 사이트와 달리 "감사"또는 "도움을 주신 분께"또는 서명을 사용하지 않습니다. "[안녕하세요, '고마워,'태그 라인 및 인사말을 게시물에서 삭제해야합니까? '(http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be)를 참조하십시오. -removed-from-posts) –