在C#中安全关闭串行COM端口 [英] Close Serial COM Port safely in C#

查看:269
本文介绍了在C#中安全关闭串行COM端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main()
{

thread_B.start();
thread_A.start();

}


public void Thread_A()
{
    while(true)
        if (State == 1)
        {
            try
            {
                Thread_B.Interrupt();
                if (!thr.Join(5000))
                {
                    Thread_B.Abort();
                    GC.Collect();
                    Thread_B.ExecutionContext.Dispose();
                    //    State = 0;
                    // From here i want to end thread safely    
                    // Application must not restart
                   
                }
                Console.WriteLine("\nInsert_Dongle");
            }
            catch (Exception e)
            { }
        }
}


Thread Thread_B= new Thread(delegate()
{
    State = TLoop();
});

public static int TLoop()
{

while(true)
// some error found
return 1;//


}




我正在线程_B中打开串行端口
如果有人在代码运行时卸下串行电缆,则传入的数据将停止,但如果我尝试关闭串行端口,串行端口仍会打开

"mscorlib.dll附加信息中出现未处理的异常system.ObjectDisposedException:安全句柄已关闭"





i am opening serial port in thread _B
if some one removes the serial cable when code is running incoming data stops but the serial port is still opened if i tried to close the serial port it gives

" unhandled exception system.ObjectDisposedException occured in mscorlib.dll addition information : Safe handle has been closed"


how to close the comport?

推荐答案

不要中止线程.
而是在SerialPort上使用超时( SerialPort.ReadTimeout属性 [ ^ ],这将导致读取操作抛出超时异常).使用信号量告诉线程何时可以终止,并使其有序地关闭.
Don''t abort the thread.
Instead, use a timeout on the SerialPort (SerialPort.ReadTimeout Property[^] which will cause the read operation to throw a timeout exception). Use a semaphore to tell the thread to terminate when it can, and let it close down in an orderly way.


http://connect.microsoft.com/VisualStudio/feedback/details/140018/serialport-crashes-after-disconnect- of-usb-com-port [
http://connect.microsoft.com/VisualStudio/feedback/details/140018/serialport-crashes-after-disconnect-of-usb-com-port[^]
has partially resolved from above link.
i am still getting IO exception for the ports , but the application is not crashing (at least for now) :)


< configuration>
< runtime>
< legacyUnhandledExceptionPolicy enabled ="1"/>
</runtime>
</configuration>

我已经尝试过了,它帮助我节省了更多的时间...
至少现在没有发生应用程序崩溃的情况...
感谢Justin Wignall(
http://stackoverflow.com/questions/3230311/problem-with-serialport [ ^ ])和汉斯·帕桑特( http://stackoverflow.com/questions/3230311/problem-with-serialport [
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="1"/>
</runtime>
</configuration>

I have tried with this and it helped me from spending more time on it...
At least no Application crashing is happening now...
Thanks to Justin Wignall(http://stackoverflow.com/questions/3230311/problem-with-serialport[^]) and Hans Passant (http://stackoverflow.com/questions/3230311/problem-with-serialport[^])


这篇关于在C#中安全关闭串行COM端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆