如何在不阻塞和等待的情况下通知主线程另一个线程上的某些消息? [英] How can I notify the main thread of some message on another thread without blocking and waiting?

查看:42
本文介绍了如何在不阻塞和等待的情况下通知主线程另一个线程上的某些消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个仅在我公司内部使用的 c# 组件.该组件封装了与特定桌面应用程序需要与之通信的许多服务器的通信.服务器可以向组件发送未经请求的消息,这些消息被捕获"在单独的线程中.

I'm writing a c# component that will only be used internally at my company. The component encapsulates communication with a number of servers that particular desktop applications need to communicate with. The servers can send unsolicited messages to the component, which are 'caught' in a separate thread.

我希望该组件的大部分内容在其创建线程的上下文中执行.我不希望单独的消息线程进行任何消息处理.相反,我想通知主线程有一条消息等待处理.想要在创建线程的上下文下执行的原因是库的用户可能不知道多线程,如果可能的话,我希望能够同步所有操作.

I want the majority of this component to execute under the context of its creating thread. I do not wish the separate message thread to do any message processing. Instead, I would like to notify the main thread that there is a message awaiting processing. The reason for wanting to execute under the context of the creating thread, is that users of the library may not know multi-threading, and I'd like to be able to synchronize all operations if possible.

是否有一种简单的方法来通知正在运行的线程?主线程会做各种不断的处理,消息线程会不停地旋转等待消息.可能值得注意的是,消息线程被封装在第三方库中.一旦收到消息,它就会执行回调.我希望回调执行诸如 mainthread.notify(message) 之类的操作.

Is there an easy way of signaling a running thread? The main thread will be doing all kinds of constant processing, and the message thread will be constantly spinning waiting for messages. It may be worth noting that the message thread is encapsulated in a third party library. Once it receives a message, it executes a callback. I'd like the callback to do something like mainthread.notify(message).

抱歉,我不太清楚我希望主线程做什么.我不希望主线程立即处理消息线程发送的消息.我希望它在不久的将来某个时间处理消息(例如 WinForms 消息循环的工作方式).

Sorry I wasn't too clear on what I want the main thread to do. I don't want the main thread to immediately process the message sent by the message thread. I want it to process the message at some time in the near future (like how the WinForms Message Loop works).

编辑 2:

场景:

Console App created on Thread1  
Thread2 created, which spins listening for Messages
Console App runs as normal
Message arrives on Thread2
Thread2 fires event MessageReady(sender, message)
Thread2 continues spinning
At the earliest convenience, Thread1 processes the message from MessageReady

我已经阅读了一些资料,似乎将代码编组到另一个线程是一种相当困难的方式,我怀疑同步此过程是否值得付出努力.

I've done some reading, and it appears that marshaling code to another thread is quite a difficult manner, and I'm doubting that synchronizing this process would be worth the effort.

推荐答案

不,这不可能.线程必须先空闲,然后才能将代码注入其中.例如,这是由 Control.BeginInvoke(Windows 窗体)或 Dispatcher.BeginInvoke(WPF)完成的.这些 UI 库通常需要在 UI 线程上执行代码,因此它们明确支持对 UI 线程进行封送处理调用.

No, that's not possible. A thread has to go idle before you can inject code into it. That's done by, for example, Control.BeginInvoke (Windows Forms) or Dispatcher.BeginInvoke (WPF). These UI libraries often require code to be executed on the UI thread so they have explicit support for marshaling calls to the UI thread.

线程处于空闲"状态很重要.如果 .NET 支持某种异步注入方法,你会遇到可怕的重入问题.

It is important for a thread to be in an "idle" state. You would have horrible re-entrancy problems if .NET supported some kind of asynchronous injection method.

这篇关于如何在不阻塞和等待的情况下通知主线程另一个线程上的某些消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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