zoukankan      html  css  js  c++  java
  • C# SerialPort System.ObjectDisposedException, safe handle has been closed 解决方案

    If I use GC.SuppressFinalize(serialPort.BaseStream), it still doesn't work in my case. Although the serial port object is not disposed by GC, the serial port can't receive data yet.

    I find another way, and it works always!

    I write a endless loop to check and create a new serial port object if it is disposed by GC.

    while (true)

    {

    Thread.Sleep(60000);

    if (serialPort == null)

    {

    serialPort = new SerialPort(portName, 9600,Parity.None,8);

    serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);

    serialPort.Open();

    }

    }

    Wish it's usefull for you guys!

  • 相关阅读:
    Project项目视图
    Console(控制台视图)
    Layout布局列表
    Layers层列表
    帐户下拉
    TransformGizmo(变换)切换
    Play播放控件
    变换工具
    工具栏
    Help帮助
  • 原文地址:https://www.cnblogs.com/newwind521/p/2113650.html
Copyright © 2011-2022 走看看