如何缓冲 .net BackgroundWorker 的输出?
我有一个来自外部源的数据流,当前在 BackgroundWorker
中收集该数据流。 每次获取另一块数据时,它都会使用 ReportProgress()
调用将该数据呈现给 GUI。
我的印象是 ProgressChanged
函数只是一种同步机制,因此当我的工作线程调用该函数时,两个线程都会在 GUI 线程处理更改时被锁定。 所以我认为问题在于,当后台线程更新 GUI 时,它无法接收任何数据,这意味着我们丢失了一些数据包。 这是正确的还是我的数据包丢失更有可能来自其他地方?
如果这是原因,那么添加第二个线程来更新 GUI 是否是一个合理的解决方案,或者是否有更好/更彻底的方法来解决我应该深入研究的这些问题?
任何想法和建议都将非常受欢迎。
I have a stream of data coming in from an external source which I currently collect in a BackgroundWorker
. Each time it gets another chunk of data, it presents that data to a GUI using a ReportProgress()
call.
I get the impression that the ProgressChanged
function is just a synchronisation mechanism though so when my worker thread calls that, both threads are locked while the GUI thread processes the change. So I think the problem is that while the background thread is updating the GUI, it can't receive any data which means we lose a few packets. Is that right or is my packet dropping more likely coming frorm elsewhere?
If that is the cause, then would adding a second thread to do the GUI updating be a reasonable solution or are there better / more thorough ways of solving these problems which I ought to delve into?
Any thoughts and suggestions would be very welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
工作线程只是向 gui 线程发送一条异步消息,这将导致 GUI 中触发事件。 它不应该停止你的后台线程。(无论如何这都不重要。如果用户决定启动另一个程序等,你的 GUI 程序可能会停止很长一段时间)
你不会谈论你的流类型正在使用。 因此,除非您接收的是不可靠的 UDP 数据报,否则此处不应丢失数据。 流是连续的。
The worker thread just sends an async message to the gui thread, which will result in an event firing in the GUI. It shouldn't halt your background thread.(and that shouldn't matter anyway. Your GUI program could halt for a long time if the user decided to start another program, etc.)
You don't talk about what kind of stream you're using. So unless you're receiving UDP datagrams, which are unreliable anyway, there should be no loss of data here. A stream is continuous .
您可能必须在 GUI 线程中创建一个 SynchronizationContext 对象,以便能够使用其 Post 方法从 BackgroundWorker 异步发送消息。
You may have to create a SynchronizationContext object in your GUI thread to be able to use its Post method to send messages asyncrhonously from your BackgroundWorker.