简体   繁体   English

C#在类之间传递消息

[英]C# passing messages between classes

I'm a bit new to C# and multithread programming in general but here goes: 我对C#和多线程编程有点新意但是这里有:

I'm working on a system where I would have an class that would be instantiated and then wait for various messages from another running class. 我正在开发一个系统,我将有一个实例化的类,然后等待来自另一个正在运行的类的各种消息。 How should I go about catching and sending these messages? 我该如何捕捉和发送这些消息?

Thanks, 谢谢,

PM 下午

if you are not using .net remoting to send the object between different appdomains. 如果您没有使用.net远程处理在不同的appdomains之间发送对象。

in simple scenarion : 简单的情节:

1- Create a singleton object of your master class. 1-创建主类的单个对象。

2- Use Event And Delegate to send the messages between classes. 2-使用Event和Delegate在类之间发送消息。

If you need to send the messages between Appdomains. 如果您需要在Appdomains之间发送消息。

1- If the Appdomain does not cross machine boundry than use IPC channel ( .Net 2.0 and later) 1-如果Appdomain不跨越机器边界而不是使用IPC通道(.Net 2.0及更高版本)

2- IF Appdomain crosses machine boundry than , you can use .net remoting. 2- IF Appdomain跨越机器边界,你可以使用.net远程处理。

Use a Producer/Consumer queue. 使用生产者/消费者队列。 There is a default implementation in .NET 4.0 and there are many sample implementations found on the net. .NET 4.0中有一个默认实现,网上有许多示例实现。 What you want, based on what you describe, is a single-producer / single-consumer queue. 根据您描述的内容,您想要的是单生产者/单个消费者队列。

What generally happens is that one thread is deemed a producer and writes messages on to a queue. 通常发生的是一个线程被视为生产者并将消息写入队列。 A separate thread, referred to as the consumer thread, is waiting idle for messages to appear on the queue. 一个单独的线程(称为使用者线程)正在等待空闲,以便在队列中显示消息。 Synchronization details as to how these threads cooperate (or not) with each other is implementation-dependent. 关于这些线程如何相互协作(或不相互)的同步细节是依赖于实现的。

If you're using .Net 3.5, run your sender in a separate thread and pass it a callback method from the listener: 如果您使用的是.Net 3.5,请在单独的线程中运行发送方,并从侦听器传递回调方法:

class Program
    {
        static void Main(string[] args)
        {
            Listener listener = new Listener();
            Sender sender = new Sender();
            sender.Send = new TalkDelegate(listener.TalkToMe);
            Thread thread = new Thread(sender.Run);
            thread.IsBackground = true;
            thread.Start();
            listener.Listen();
        }
    }

    delegate bool TalkDelegate(string messageArg);

    class Listener
    {
        bool keepListening = true;

        public bool TalkToMe(string messageArg)
        {
            Console.WriteLine(messageArg);
            return keepListening;
        }

        public void Listen()
        {
            DateTime startTime = DateTime.Now;
            while ((DateTime.Now - startTime) < TimeSpan.FromSeconds(10))
            {
                Thread.Sleep(100);
            }
            keepListening = false;
        }
    }

    class Sender
    {
        public TalkDelegate Send;

        public void Run()
        {
            bool keepTalking = true;
            while (keepTalking)
            {
                keepTalking = Send(DateTime.Now.ToString("HH:mm:ss"));
                Thread.Sleep(1000);
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM