什么是两个独立的C#.NET应用程序来互相交谈在同一台计算机上的最简单的方法最简单、机上、应用程序、独立

2023-09-03 13:38:41 作者:分享一组丧得要死的,愿你可爱一点,快乐一点。

我想我的两个应用程序能够将字符串发送给对方,并根据不同的字符串的做一些事情的。

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

这是一个的 pre-证明型概念样机的东西类型,因此没有安全precautions必要的,只要你想效率低下。

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)

推荐答案

在何种程度上,他们的真的,真的的需要是不同的应用程序?

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();
}

在这一点上,你可以使用静态变量(在第四个项目,双方前两个项目的引用)来设置他们两个人之间的共享通信信道。你可以有两个生产者/消费者队列(看半路页) ,每个方向一个,例如。 (你会想使队列只是使用字符串,或者做一个普通的一个,然后用 ProducerConsumer<字符串> 例如,如果您可以使用.NET 4.0测试版,您可以使用 BlockingCollection<字符串&GT ;。

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>.)

这是的非常哈克的 - 甚至没有不同的的AppDomain的隔离是你可以看到在静态变量而言访问一些有趣的效果从两个应用程序但这是有效的你想什么来实现的。

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.

您绝不应借此实现想法的的任何地方的接近生产code - 但你所描述的情况,这听起来像容易实现是最重要的一点。

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.

以防万一项目层次是没有意义的,这里有一个图形化的重新presentation

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.)