两个独立的C#.Net应用程序在同一台计算机上相互通信的最简单方法是什么

时间:2022-01-31 13:02:51

I would like my two applications to be able to send strings to each other and depending on the string "do something".

我希望我的两个应用程序能够相互发送字符串,并取决于字符串“做某事”。

This is for a pre-proof-of-concept-mockup type of thing so no security precautions needed and as inefficient as you want.

这是针对事前概念模型类型的事情,因此不需要安全预防措施,也不需要你想要的低效率。

So how do I do this with a minimum of work on my part.

那么我如何以最少的工作来做到这一点。

(You my dear fellow so user can work as hard as you please on the problem)

(你亲爱的家伙,所以用户可以在问题上努力工作)

7 个解决方案

#1


To what extent do they really, really need to be different applications?

他们到底在多大程度上确实需要不同的应用程序?

Could you have two separate projects which you launch from a third project, on different threads?

您是否可以在不同的线程上从第三个项目启动两个单独的项目?

static void Main()
{
    new Thread(Project1.Program.Main).Start();
    new Thread(Project2.Program.Main).Start();
}

At that point you could use static variables (in a fourth project, referenced by both of the first two projects) to set up a shared communication channel between the two of them. You could have two producer/consumer queues (look half way down the page), one for each direction, for example. (You'd want to make the queue just use strings, or make a generic one and then use ProducerConsumer<string> for example. If you can use the .NET 4.0 beta, you could use a BlockingCollection<string>.)

此时,您可以使用静态变量(在第四个项目中,由前两个项目引用)在两个项目之间建立共享通信通道。例如,您可以有两个生产者/消费者队列(在页面的下半部分),每个方向一个。 (你想让队列只使用字符串,或者制作一个通用的队列,然后使用ProducerConsumer 。如果你可以使用.NET 4.0 beta,你可以使用BlockingCollection 。)

That would be extremely hacky - and without the isolation of even different AppDomains you could see some interesting effects in terms of static variables accessed from both "applications" but that's effectively what you're trying to achieve.

这将是非常hacky - 即使没有甚至不同的AppDomain隔离,你可以看到从两个“应用程序”访问的静态变量的一些有趣的效果,但这实际上是你想要实现的。

You should in no way take this implementation idea anywhere near production code - but for the situation you're describing, it sounds like "simple to implement" is the most important point.

你不应该在生产代码附近的任何地方采用这种实现方法 - 但对于你所描述的情况,听起来“简单实现”是最重要的一点。

Just in case the project hierarchy doesn't make sense, here's a graphical representation

如果项目层次结构没有意义,这里是一个图形表示

        Launcher
       /        \
      /          \
   App 1       App 2
      \          /
       \        /
        \      /
      Shared stuff

(To make it even simpler you could actually just use one project, and make its main method launch two different threads using methods within the same project. The idea of it being two applications is all smoke and mirrors by this point anyway. On the other hand, it might make it easier to think about the different apps by separating the projects.)

(为了使它更简单,你实际上可以只使用一个项目,并使其主方法使用同一项目中的方法启动两个不同的线程。无论如何,它是两个应用程序的想法都是烟雾和镜像。另一方面。一方面,它可以通过分离项目来更容易地思考不同的应用程序。)

#2


There are several mechanisms for this - probably the easiest to use is named pipes. I have not used it, but I understand the Windows Communication Foundation (WCF) is easy to use as well.

有几种机制 - 可能最容易使用的是命名管道。我没有使用它,但我知道Windows Communication Foundation(WCF)也很容易使用。

There are a bunch of articles on CodePoject about WCF :)

有关WCF的CodePoject上有很多文章:)

An advantage of using WCF is that it would let easily move your processes to different systems. (should that be practical for your scenario).

使用WCF的一个优点是可以轻松地将您的流程移动到不同的系统。 (这应该适用于您的场景)。

#3


One way is to use .NET Remoting IpcChannel class to communicate across different processes on the same machine.

一种方法是使用.NET Remoting IpcChannel类在同一台机器上的不同进程之间进行通信。

#4


WCF is a good way to good, even if once you start delving into it then it can become complicated.

WCF是一种很好的方式,即使你开始深入研究它,它也会变得复杂。

You'd define a simple contract that accepts a string, then implement the contract in each application, doing whatever you want when the string arrives. Within an application you can self host the service, meaning the application itself hosts the service - so you don't need IIS. Host them on known ports, add a web service reference in each solution and away you go.

您定义了一个接受字符串的简单契约,然后在每个应用程序中实现契约,在字符串到达​​时执行您想要的任何操作。在应用程序中,您可以自行托管服务,这意味着应用程序本身托管服务 - 因此您不需要IIS。将它们托管在已知端口上,在每个解决方案中添加Web服务引用,然后离开。

#5


There are a lot of very easy ways for apps to communicate in very simplistic ways. I think some of the easiest would be the following

应用程序有很多非常简单的方式以非常简单的方式进行通信。我认为最简单的一些是以下内容

  • Named Pipe
  • Local Sockets
  • Shared Memory

#6


The easiest approach I can think of is creating a file that the other process checks for.

我能想到的最简单的方法是创建一个其他进程检查的文件。

A more "advanced" and "sensible" way would be to use sockets and localhost.

更“先进”和“明智”的方法是使用套接字和localhost。

And if I would want to actually implement it I would try and learn about proper IPC.

如果我想实际实现它,我会尝试学习正确的IPC。

#7


I'd go with sockets.

我会选择插座。

#1


To what extent do they really, really need to be different applications?

他们到底在多大程度上确实需要不同的应用程序?

Could you have two separate projects which you launch from a third project, on different threads?

您是否可以在不同的线程上从第三个项目启动两个单独的项目?

static void Main()
{
    new Thread(Project1.Program.Main).Start();
    new Thread(Project2.Program.Main).Start();
}

At that point you could use static variables (in a fourth project, referenced by both of the first two projects) to set up a shared communication channel between the two of them. You could have two producer/consumer queues (look half way down the page), one for each direction, for example. (You'd want to make the queue just use strings, or make a generic one and then use ProducerConsumer<string> for example. If you can use the .NET 4.0 beta, you could use a BlockingCollection<string>.)

此时,您可以使用静态变量(在第四个项目中,由前两个项目引用)在两个项目之间建立共享通信通道。例如,您可以有两个生产者/消费者队列(在页面的下半部分),每个方向一个。 (你想让队列只使用字符串,或者制作一个通用的队列,然后使用ProducerConsumer 。如果你可以使用.NET 4.0 beta,你可以使用BlockingCollection 。)

That would be extremely hacky - and without the isolation of even different AppDomains you could see some interesting effects in terms of static variables accessed from both "applications" but that's effectively what you're trying to achieve.

这将是非常hacky - 即使没有甚至不同的AppDomain隔离,你可以看到从两个“应用程序”访问的静态变量的一些有趣的效果,但这实际上是你想要实现的。

You should in no way take this implementation idea anywhere near production code - but for the situation you're describing, it sounds like "simple to implement" is the most important point.

你不应该在生产代码附近的任何地方采用这种实现方法 - 但对于你所描述的情况,听起来“简单实现”是最重要的一点。

Just in case the project hierarchy doesn't make sense, here's a graphical representation

如果项目层次结构没有意义,这里是一个图形表示

        Launcher
       /        \
      /          \
   App 1       App 2
      \          /
       \        /
        \      /
      Shared stuff

(To make it even simpler you could actually just use one project, and make its main method launch two different threads using methods within the same project. The idea of it being two applications is all smoke and mirrors by this point anyway. On the other hand, it might make it easier to think about the different apps by separating the projects.)

(为了使它更简单,你实际上可以只使用一个项目,并使其主方法使用同一项目中的方法启动两个不同的线程。无论如何,它是两个应用程序的想法都是烟雾和镜像。另一方面。一方面,它可以通过分离项目来更容易地思考不同的应用程序。)

#2


There are several mechanisms for this - probably the easiest to use is named pipes. I have not used it, but I understand the Windows Communication Foundation (WCF) is easy to use as well.

有几种机制 - 可能最容易使用的是命名管道。我没有使用它,但我知道Windows Communication Foundation(WCF)也很容易使用。

There are a bunch of articles on CodePoject about WCF :)

有关WCF的CodePoject上有很多文章:)

An advantage of using WCF is that it would let easily move your processes to different systems. (should that be practical for your scenario).

使用WCF的一个优点是可以轻松地将您的流程移动到不同的系统。 (这应该适用于您的场景)。

#3


One way is to use .NET Remoting IpcChannel class to communicate across different processes on the same machine.

一种方法是使用.NET Remoting IpcChannel类在同一台机器上的不同进程之间进行通信。

#4


WCF is a good way to good, even if once you start delving into it then it can become complicated.

WCF是一种很好的方式,即使你开始深入研究它,它也会变得复杂。

You'd define a simple contract that accepts a string, then implement the contract in each application, doing whatever you want when the string arrives. Within an application you can self host the service, meaning the application itself hosts the service - so you don't need IIS. Host them on known ports, add a web service reference in each solution and away you go.

您定义了一个接受字符串的简单契约,然后在每个应用程序中实现契约,在字符串到达​​时执行您想要的任何操作。在应用程序中,您可以自行托管服务,这意味着应用程序本身托管服务 - 因此您不需要IIS。将它们托管在已知端口上,在每个解决方案中添加Web服务引用,然后离开。

#5


There are a lot of very easy ways for apps to communicate in very simplistic ways. I think some of the easiest would be the following

应用程序有很多非常简单的方式以非常简单的方式进行通信。我认为最简单的一些是以下内容

  • Named Pipe
  • Local Sockets
  • Shared Memory

#6


The easiest approach I can think of is creating a file that the other process checks for.

我能想到的最简单的方法是创建一个其他进程检查的文件。

A more "advanced" and "sensible" way would be to use sockets and localhost.

更“先进”和“明智”的方法是使用套接字和localhost。

And if I would want to actually implement it I would try and learn about proper IPC.

如果我想实际实现它,我会尝试学习正确的IPC。

#7


I'd go with sockets.

我会选择插座。