问题描述
你好,
我有一个项目,在其中实现了一个功能,该功能通过串行通信将数据发送到外部设备.我必须等待回应.如果在合理的时间内未收到响应,则该函数返回错误;如果响应良好,则返回OK.
问题是,如何使主线程暂停并保持事件正常运行.我可以通过创建一个新的线程来发送数据来实现这一点,然后等到主线程的ManualResetEvent将其重置.但是据我所知,此方法涉及许多标志,以表明哪个响应对每个线程有效.
有一种简单的方法吗?
Hello,
I have a project where I implement a function that sends a data over serial communication to external device. I have to wait for the response. If the response doesn''t come in a reasonable time then the function returns an error, If the response is good, returns OK.
The question is, how do I make the main thread to halt and keep the events working. I can implement this by making new thread that sends the data, than waits until ManualResetEvent from the main thread resets it. But as far as I know this method involves many flags to signal which response is valid for each thread.
Is there an easy method???
推荐答案
这篇关于挂起主线程并等待C#中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!