如何让两个进程进行通信?

发布于 2024-11-24 22:45:18 字数 273 浏览 1 评论 0原文

我有两个 .net 应用程序。一个是普通的 Windows 窗体应用程序,另一个是 Microsoft Word COM 加载项。我正在使用 C# 开发这两个应用程序。

我需要这两个应用程序来相互通信。我想知道实现这一目标的最佳方法是什么。

我想到的第一件事是我应该使用双向命名管道来执行此操作,但命名管道是系统范围的,我需要将连接限制为同一会话中的进程运行(这可以并且将用于终端服务器)。

有什么方法可以将命名管道限制在当前会话中吗?如果没有,我还有什么选择?

谢谢

I have two .net applications. One is a normal Windows Forms application while the other is a Microsoft Word COM Add-in. I'm developping both applications with C#.

I need theses two application to comunicate with each other. I'm wondering what is the best way to achieve this.

The first thing I though was that I should use a bi-directional named pipe to do this, but named pipes are system-wide and I need the connection to be restricted to process runnings in the same session (This could and will be used on a terminal server).

Is there any way to restrict a named pipe to the current session? If there is not what alternatives do I have?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-12-01 22:45:18

您可以创建本地 Web 服务来实现此目的。

要创建 Web 服务,您必须执行类似以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService1
{        
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        public int myInt = 0;

        [WebMethod]
        public int increaseCounter()
        {
            myInt++;
            return myInt;
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

    }
}

当您运行 Web 服务时,您应该看到类似以下内容:

在此处输入图像描述

在不同的程序/线程(在本例中为控制台应用程序)上,

您应该能够连接到该服务:

enter image此处描述

在此处输入图像描述

输入图像description here

最后输入您刚刚创建的服务的 url:

在此处输入图像描述

现在您可以实例化一个来自该类的对象此控制台应用程序的 Service1 如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication36
{
    class Program
    {
        static void Main(string[] args)
        {
            localhost.Service1 service = new localhost.Service1();

            // here is the part I don't understand..
            // from a regular class you will expect myInt to increase every time you call
            // the increseCounter method. Even if I call it twice I always get the same result.

            int i;
            i=service.increaseCounter();

            Console.WriteLine(i.ToString());

            // you can recive string data as:
            string s = service.HelloWorld();

            // output response from other program
            Console.WriteLine(s);

            Console.Read();


        }
    }
}

使用此技术,您将能够将大部分任何内容传递到其他应用程序(任何可序列化的内容)。因此,也许您可​​以将此 Web 服务创建为第三个线程,以使其更有组织性。希望这有帮助。

you can create a local web service to achieve this.

To create your web service you must do something similar to:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService1
{        
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        public int myInt = 0;

        [WebMethod]
        public int increaseCounter()
        {
            myInt++;
            return myInt;
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

    }
}

when you run your web service you should see something like:

enter image description here

on a different program/thread (in this case console application)

you should be able to connect to that service as:

enter image description here

enter image description here

enter image description here

Lastly type the url of the service you just created:

enter image description here

Now you are able to instantiate an object from that class Service1 from this console application as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication36
{
    class Program
    {
        static void Main(string[] args)
        {
            localhost.Service1 service = new localhost.Service1();

            // here is the part I don't understand..
            // from a regular class you will expect myInt to increase every time you call
            // the increseCounter method. Even if I call it twice I always get the same result.

            int i;
            i=service.increaseCounter();

            Console.WriteLine(i.ToString());

            // you can recive string data as:
            string s = service.HelloWorld();

            // output response from other program
            Console.WriteLine(s);

            Console.Read();


        }
    }
}

with this technique you will be able to pass mostly anything to your other application (anything that is serializable). so maybe you can create this webservice as a third thread to make it more organized. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文