为什么在 SerialPort.Open 和 Close 之前使用 Thread.Sleep()? [英] Why Thread.Sleep() before SerialPort.Open and Close?

查看:41
本文介绍了为什么在 SerialPort.Open 和 Close 之前使用 Thread.Sleep()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到我在 SerialPort 关闭和打开时阅读的示例代码,人们会在 SerialPort.Open() 和 Close() 之前添加 Thread.Sleep(1000).像下面这样:

I notice sample codes I read on SerialPort closing and opening where people would add Thread.Sleep(1000) before SerialPort.Open() and Close(). Like the following:

Thread.Sleep(1000);
serialPort1.Open();
/* some code */
Thread.Sleep(1000);
serialPort1.Close();

我在任何地方都找不到任何解释.为什么人们在串口打开或关闭之前使用 Thread.Sleep 阻塞它?是出于某种计时目的吗?每当我读取或写入串行端口时,我是否应该放置一个 Thread.Sleep ?

I cannot find any explanation anywhere. Why do people block the serial port using Thread.Sleep before it gets Open or Close? Is it for some timing purpose? Am I supposed to put a Thread.Sleep whenever I read from or write to Serial Port as well?

推荐答案

当您打开一个端口时,SerialPort 类会在后台启动一个新线程,该线程负责(通过 WaitCommEvent Windows API 函数)等待串行端口活动(例如数据到达)并向您的处理程序触发适当的事件.这就是像 DataReceived 这样的事件实际上发生在辅助线程上的原因.

When you open a port, the SerialPort class fires up a new thread under the hood which is responsible (via the WaitCommEvent Windows API function) for waiting for serial port activity (e.g. data arrived) and firing the appropriate events to your handlers. That's why events like DataReceived actually occur on a secondary thread.

当您关闭端口时,Close() 调用会立即返回,但辅助线程需要一些时间才能停止运行.

When you close the port, the Close() call returns immediately but it takes some time for the secondary thread to spin down.

如果您在调用 Close 后过快地尝试重新打开端口,并且线程尚未停止旋转,则 SerialPort 实例未处于可以开始新连接的状态.

If you attempt to re-open the port too quickly after calling Close, and the thread hasn't yet spun down, then the SerialPort instance is not in a state where it can begin a new connection.

请注意 串行端口的 MSDN 文档.关闭状态:

任何应用程序的最佳做法是等待一定数量的在尝试调用 Open 之前调用 Close 方法后的时间方法,因为端口可能不会立即关闭.

The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly.

您可以跟踪关闭端口的时间,并在再次打开它之前确保已经过了一些任意的超时时间.

You could keep track of when you closed the port, and before opening it again make sure some arbitrary timeout has elapsed.

在读/写之前不需要睡觉,但要记住一些怪癖:

No need to sleep before reads / writes, although a few quirks to keep in mind:

请记住,.NET BCL 中的 SerialPort 类仍然依赖于底层的 Win32 API,而且我认为自最初实现以来,它并没有受到微软的喜爱.

Keep in mind the SerialPort class in the .NET BCL still relies on the underlying Win32 API, and I don't think it's gotten a lot of love from Microsoft since the initial implementation.

有关更多信息,请参阅:

For more information see:

http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/f334ecce-eca3-46fd-8b65-27c02a1d4fea#10dae30d-bc75-49bb-a669-78ea39e>

http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/f334ecce-eca3-46fd-8b65-27c02a1d4fea#10dae30d-bc75-49bb-a669-79399e58e1cc

SerialPort 类偶尔会在 Dispose 上挂起

这篇关于为什么在 SerialPort.Open 和 Close 之前使用 Thread.Sleep()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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