如何在.NET中的非UI线程中实现消息泵? [英] How to implement a message pump in Non-UI thread in .NET?

查看:94
本文介绍了如何在.NET中的非UI线程中实现消息泵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读此博客:

I am reading this blog: Use Threads Correctly, and I am wondering:

如何在非UI线程中实现消息(注意:我的意思不是Windows消息)?

How to implement a message (Note: I don't mean Windows Message here) pump in a non-UI thread?

我想要的是消息可以是对象或命令,例如Action<T>/Func<T>等. 是否必须对不同类型的消息使用单独的队列?对对象说一个队列,对Action<T>/Func<T>说一个队列?

What I want is that the message can be an object or a command, say an Action<T>/Func<T>, etc. Do I have to use separate queue for different type of message? Say one queue for object, one queue for Action<T>/Func<T>?

鉴于消息的类型各不相同,如何实现?

Given that the type of messages vary, how to implement it?

我要完成的是生产者/消费者模型,用于通信的生产者/消费者共享队列,该队列不仅可以包含供消费者使用的对象,而且还可以使用一些"命令"传递给使用者执行.

What I want to complete is a producer/consumer model, producer/consumer share queue for communication, the queue can not only contain object for the consumer to consume, but also some 'command' can be passed to the consumer for execution.

推荐答案

为代码格式设置一个单独的答案

Made this a seperate answer for code formatting

好吧,阅读完更新后,我想您只是想要我在第二种情况"中描述的内容

Ok so after reading your update I think you want what I describe in the "second case" you simply want

Broadcast<T>("Foo") 

其中T是代表.

那么您的消费者就会这样做

Then your consumer will do

Subscribe<T>("Foo",HandlerMethod)

因此生产者消费者场景将如下所示

So a producer consumer scenario would look like this

internal static class MessagePump
    {

        public static void Subscribe<T>(String foo, Action<String> handlerMethod)
        {
            throw new NotImplementedException();
        }

        public static void BroadcastMessage<T>(String foo, Action<String> someAction)
        {
            throw new NotImplementedException();
        }
    }

    public class Producer
    {
        void SendMessage()
        {
            MessagePump.BroadcastMessage<Action<String>>("Foo", SomeAction);
        }

        void SomeAction(String param)
        {
            //Do Something
        }
    }


    public class Consumer
    {

        public Consumer()
        {
            MessagePump.Subscribe<Action<String>>("Foo", HandlerMethod);
        }

        void HandlerMethod(String param)
        {
            // Do Something
        }

    }

这只是我脑海中不可思议的事,是一个虚构的示例,因此请带一点盐.这几乎完全是我在之前发布的快递框架中所做的事情.您可能需要深入研究该代码,以获得更具体的实现示例.

This is just something off the top of my head and is a contrived example so take it with a grain of salt. This is nearly exactly what I am doing in the courier framework I posted earlier. You may want to dive into that code to get a more concrete implementation example.

您需要考虑如何管理使用者,如何验证广播和订阅,以及如何针对特定情况来确保正确调用要传递的委托?还是在乎?

You need to think about how you will manage consumers, how you validate Broadcast and subscriptions and for your specific case how are you going to ensure the delegate you are passing around is invoked correctly? Or do you care?

有帮助吗?

这篇关于如何在.NET中的非UI线程中实现消息泵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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