2009-12-26 6 views
0

직렬 포트를 통해 정보를 보내는 장치로부터 데이터를 수신하고 있는데 "ObjectDisposedException 안전 손잡이가 닫혔습니다"라는 예외가 발생합니다. 몇 초에서 수 분 내에 발생할 수 있으며 패턴이없는 것처럼 보입니다. 또 다른 일은 예외없이 데이터 수신을 멈추고 VS IDE 출력 창 탭에서 스레드가 종료되었음을 알 수 있습니다. 나는 그것이 왜 그렇게하고 있는지 전혀 모른다. 여기직렬 포트 및 ObjectDisposedException 안전 손잡이가 닫혔습니다

내가 직렬 포트를 만드는 방법은 다음과 같습니다

DeviceSerialPort serialport = new DeviceSerialPort("COM1", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One); 
serialport.RHDataReceived += new EventHandler<DeviceDataEventArgs>(SerialPort_RHDataReceived); 
serialport.Open(); 

을 그리고 여기 직렬 포트 코드 :

당신의 스레드가 매우 짧은 살고있다
namespace Instrument 
{ 
    public class DeviceSerialPort 
    { 
     public event EventHandler<DeviceDataEventArgs> RHDataReceived; 

     private SerialPort _serialPort; 
     private byte[] _readBuffer = new byte[1024]; 

     public DeviceSerialPort(string portName, int baudRate, Parity parity, 
      int dataBits, StopBits stopBits) 
     { 
      _serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits); 
      _serialPort.DataReceived +=new SerialDataReceivedEventHandler(SerialPort_DataReceived); 

     } 

     public void Open() 
     { 
      _serialPort.Open(); 
     } 

     private void SerialPort_DataReceived(object sender, EventArgs e) 
     { 
      //SerialPort sp = (SerialPort)sender; 

      int bytesRead = _serialPort.Read(_readBuffer, 0, _readBuffer.Length); 


      if (_readBuffer[0] == 5) 
      { 
       onRHDataReceivedEvent(new DeviceDataEventArgs(_readBuffer[0], _readBuffer[1], _readBuffer[2], _readBuffer[3])); 
      } 
     } 


     protected void onRHDataReceivedEvent(DeviceDataEventArgs e) 
     { 
      if (RHDataReceived != null) 
      { 
       RHDataReceived(this, e); 
      } 
     } 

    } 

    public class DeviceDataEventArgs : EventArgs 
    { 
     public byte Command { get; set; } 
     public byte Data0 { get; set; } 
     public byte Data1 { get; set; } 
     public byte Crc { get; set; } 

     public DeviceDataEventArgs(byte cmd, byte data0, byte data1, byte crc) 
     { 
      Command = cmd; 
      Data0 = data0; 
      Data1 = data1; 
      Crc = crc; 
     } 
    } 
} 

답변

1

, 그것은 시간의 정확히 길이 산다 직렬 포트를 열면 완료됩니다. 그리고 그렇게되면 직렬 포트가 폐기됩니다.

이것이 내가 보는 방법입니다. 이벤트 기반 접근 방식 (DataReceived 이벤트 처리)을 사용하고 있습니다.이 경우 프레임 워크 및 OS 리소스 측면에서 비용이 많이 드는 다른 스레드가 필요하지 않습니다. 귀하의 경우에는 Thread 개체를 제거하고 serialPort.Open()으로 직접 전화하십시오. 기존 스레드에 DataReceived 이벤트가 수신됩니다.

AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledException_Handler 

을 그리고 처리기를 추가 :

+0

예, 바로! 그것은 조기 종료 스레드 문제를 해결하는 것 같았다. 그것은 지금 멈추지 않고 계속적으로 달리고 있습니다. –

+1

그러나 "ObjectDisposedException 안전 핸들이 닫혔습니다."가 여전히 발생합니다. –

+0

DeviceSerialPort 객체를 만드는 코드에서 약간 축소하십시오. 신청 기간 동안 계속 살아 있나? –

-1

사용하여 form_load에서 처리되지 않은 예외 핸들러를 등록

'This sub does the magic 
Private Sub UnhandledException_Handler(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs) 
    Dim ex As Exception = DirectCast(args.ExceptionObject, Exception) 
    Thread.CurrentThread.Suspend() 
End Sub 
+0

질문은 C#에서, 당신은 도움이되지 않습니다 – Winter