处理来自另一个应用程序的事件 [英] Handle events from another application

查看:99
本文介绍了处理来自另一个应用程序的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hellu

这个问题很简单,但是答案似乎很多种而且还不清楚.

The question is simple, yet the answers seem to be various and pretty unclear.

我如何在应用程序A中处理应用程序B发送的事件,以及如何在B中实现这些事件?知道B不知道A的存在.

How can I, in an application A, handle events sent by an application B, and how to implements those events in B? Knowing that B has no idea of the existence of A.

比方说,B是一种批处理"类型的程序,在后台执行任何操作都没有任何图形显示,而A是很好的GUI (双关语意),它应该编写消息B通过对话或其他方式将他发送出去.

Let's say B is a "batch-like" type of program, doing things in the back with no graphical display whatsoever, and A is the nice GUI (pun intended) that should write messages B sends him in a dialog or something.

我找到了有关此问题的有趣答案(例如

I found interesting answers about this (like this one or this one), but none actually answers the issue with example for C#; at best, they redirect to some external tools.

我听说这是C#中的问题,但是最后,有没有一种干净的方法来做到这一点?谢谢.

I heard it was an issue in C#, but in the end, is there a clean way to do it? Thanks.

如果这可以缓解问题,那么假设A启动B(使用Process或其他方法),因为知道B也可以单独启动(在这种情况下,我不需要整个事件处理工作).

If that can ease the thing, let's say A launches B (with Process or something), knowing that B can also be launched alone (in which case, I don't need this whole event handling thing).

推荐答案

您可以使用MemoryMappedFileMutexEventWaitHandle的组合来在进程之间进行管理.

You could use a combination of MemoryMappedFile, Mutex and EventWaitHandle to manage this between processes.

以下示例显示了它的工作方式.在实际代码中,producer()方法将与consumer()方法位于不同的应用程序中,但这只是为了说明一种实现方式:

The following example shows how it would work. In real code, the producer() method would be in a different application from the consumer() method, but this serves to illustrate an implementation:

using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        void run()
        {
            Task.Run(() => consumer());
            Task.Run(() => producer());

            Console.WriteLine("Press <ENTER> to stop.");
            Console.ReadLine();
        }

        static void producer()
        {
            using (var mmf = MemoryMappedFile.CreateOrOpen("MyMapName", 1024))
            using (var view = mmf.CreateViewStream())
            {
                var writer = new BinaryWriter(view);
                var signal = new EventWaitHandle(false, EventResetMode.AutoReset, "MyEventName");
                var mutex = new Mutex(false, "MyMutex");

                for (int i = 0; i < 100; ++i)
                {
                    string message = "Message #" + i;

                    mutex.WaitOne();
                    writer.BaseStream.Position = 0;
                    writer.Write(message);
                    signal.Set();
                    mutex.ReleaseMutex();

                    Thread.Sleep(1000);
                }
            }
        }

        static void consumer()
        {
            using (var mmf = MemoryMappedFile.CreateOrOpen("MyMapName", 1024))
            using (var view = mmf.CreateViewStream())
            {
                var reader = new BinaryReader(view);
                var signal = new EventWaitHandle(false, EventResetMode.AutoReset, "MyEventName");
                var mutex = new Mutex(false, "MyMutex");

                while (true)
                {
                    signal.WaitOne();
                    mutex.WaitOne();
                    reader.BaseStream.Position = 0;
                    var message = reader.ReadString();
                    Console.WriteLine("Received message: " + message);
                    mutex.ReleaseMutex();
                }
            }
        }

        static void Main()
        {
            new Program().run();
        }
    }
}

重要提示:此实现不使用队列,因此,如果消费者在收到新消息之前不处理消息,则有可能会丢失消息.

IMPORTANT: This implementation does not use a queue, so it is possible for the consumer to miss messages if it does not process them before a new message arrives.

如果您一定不要错过任何消息,则必须使用某种排队实施,例如 MSMQ .

If you must not miss any messages, you would have to use some kind of queuing implementation instead, for example MSMQ.

但是,您也可以考虑使用 Windows Communications Foundation ,允许您在另一个进程中调用远程方法-但这可能需要多个进程之间的耦合,而不是您想要的.

However, you could also consider using Windows Communications Foundation, which allows you to call remote methods in another process - but that might require more coupling between processes than you want.

最后,另一种可能性是使用 Named Pipe .我实际上认为这可能是最适合您的解决方案.

Finally, another possibility is to use a Named Pipe. I actually think this might be the best solution for you.

这里有一个Microsoft示例,显示了使用命名管道的服务器和客户端IPC:

There's a Microsoft example showing server and client IPC using a named pipe here: http://msdn.microsoft.com/en-us/library/bb546085%28v=vs.110%29.aspx

这篇关于处理来自另一个应用程序的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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