挂起主线程并等待C#中的事件 [英] Suspend main thread and waiting for events in C#

查看:516
本文介绍了挂起主线程并等待C#中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个项目,在其中实现了一个功能,该功能通过串行通信将数据发送到外部设备.我必须等待回应.如果在合理的时间内未收到响应,则该函数返回错误;如果响应良好,则返回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???

推荐答案

为什么要暂停主线程?为什么要等?

您不想停止主线程:这将完全停止GUI,并且用户受够了,并向三指致敬.

您可以将函数移到BackgroundWorker,但是当它失败(或成功)时,您需要调用主线程来报告所有内容.

相反,应处理SerialPort.DataRecieved事件以捕获响应,并设置一个计时器以允许期望外部设备响应的时间.
这样,UI仍然有效,您可以显示进度"消息以使用户保持了解状态,并且一切都按原本打算的方式进行...
Why halt the main thread? Why wait at all?

You don''t want to halt the main thread: that stops the GUI completely, and the user gets fed up and gives the three finger salute.

You could move your function to a BackgroundWorker, but then when it fails (or succeeds) you need to invoke the main thread to report anything.

Instead, handle the SerialPort.DataRecieved event to catch the response, and set up a timer to allow as long as the external device is expected to take to respond.
That way, the UI still works, you can show "progress" messages to keep the user informed, and it all works the way it was meant to...


您应该永远不要停止主线程,甚至不要在其中进行任何冗长的处理.另外,根据经验,请勿使用Application.DoEvents.您需要做几乎相反的事情.您应该在单独的线程中运行所有通信.与UI的任何通信均应通过Control.InvokeControl.BeginInvokeDispatcher.InvokeDispatcher.BeginInvoke完成.

另请参见: Control.Invoke()与Control.BeginInvoke() [ ^ ], Treeview扫描仪和MD5问题 [ ^ ]通常使用线程的变体.

是的,这个很简单.甚至都不要放弃没有多线程的想法,这将是一场噩梦,并且永远不会为您提供可接受的解决方案.

—SA
You should never halt main thread or even do any lengthy processing in it. Also, as a rule of thumb, never use Application.DoEvents. You need to do nearly the opposite. You should run all your communications in a separate thread. Any communications with UI should be done via Control.Invoke, Control.BeginInvoke, Dispatcher.Invoke or Dispatcher.BeginInvoke.

See also: Control.Invoke() vs. Control.BeginInvoke()[^], Problem with Treeview Scanner And MD5[^] on generally used variants of using threads.

Yes, this is easy enough. Don''t even play with the idea to go without multi-threading, it will be a nightmare and never got you the acceptable solution.

—SA


创建一个发送数据并等待响应的线程.如果发生超时,请让线程发布自定义失败"事件.如果成功,则将其发布自定义成功"事件.

这样,UI可以在线程等待响应时顺其自然,并且可以在线程发布事件时(如果已发布)进行处理.
Create a thread that sends the data and waits for the response. If the timeout occurs, have the thread post a custom "failure" event. If it succeeds, have it post a custom "success" event.

That way, the UI can chug along while the thread is wating for a response, and can handle the events posted by the thread when/if they''re posted.


这篇关于挂起主线程并等待C#中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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