简体   繁体   English

使用命名管道通过 NamedPipeWrapper 在 Winforms 和 Windows 服务应用程序之间进行通信

[英]Using Named Pipes to communicate between Winforms and Windows service applications with NamedPipeWrapper

I'm attempting to communicate between a winforms app and a windows service using NamedPipeWrapper ( https://github.com/acdvorak/named-pipe-wrapper ) and I' having trouble with the server side, which is the service.我正在尝试使用 NamedPipeWrapper ( https://github.com/acdvorak/named-pipe-wrapper ) 在 winforms 应用程序和 Windows 服务之间进行通信,但我在服务器端(即服务)遇到了问题。

The code is in there to recieve messages, however when the service is running normally, nothing happens and running through debug on VS tells me the code will never be reached.代码在那里接收消息,但是当服务正常运行时,没有任何反应并且在 VS 上运行调试告诉我永远无法访问代码。 The only way i can have it work is ifi create a function that means the service is always continuously working:我让它工作的唯一方法是创建一个函数,这意味着该服务始终持续工作:

        public NamedPipeServer<string> server = new NamedPipeServer<string>("NamedPipe");
        private readonly ISet<string> client = new HashSet<string>();

public WinService()
        {
            InitializeComponent();

            server.ClientConnected += OnClientConnected;
            server.ClientDisconnected += OnClientDisconnected;
            server.ClientMessage += OnClientMessage;
            server.Error += OnError;
            server.Start();
            while (KeepRunning)
            { }
            server.Stop();
        }

        private bool KeepRunning
        {
            get
            {
                if (sc.Status.ToString() == "Running")
                    return false;
                return true;
            }
        }

The example code on the github article shows this for a console application to keep it shutting down, but I'm worried it will stop the rest of the service running (I'm using a timer to trigger events). github 文章上的示例代码显示了控制台应用程序的这一点,以使其保持关闭状态,但我担心它会停止其余服务的运行(我正在使用计时器来触发事件)。

Would this system cause the timer to stop running, and is there any method where i can keep the service waiting for a message from the named pipe without resorting to using this method?该系统是否会导致计时器停止运行,是否有任何方法可以让服务等待来自命名管道的消息而不诉诸于使用此方法? it seems to use a lot of memory with this method(~300mb), and some of the servers i'm planning on adding this to might not have that spare.这种方法似乎使用了大量内存(~300mb),而且我计划将其添加到的一些服务器可能没有多余的内存。

I'm attempting to communicate between a winforms app and a windows service using NamedPipeWrapper ( https://github.com/acdvorak/named-pipe-wrapper ) and I' having trouble with the server side, which is the service.我正在尝试使用 NamedPipeWrapper ( https://github.com/acdvorak/named-pipe-wrapper ) 在 winforms 应用程序和 Windows 服务之间进行通信,但我在服务器端(即服务)遇到了问题。

Debugging services while writing them is notoriously hard.在编写服务的同时调试服务是出了名的困难。 The way they have to be registered and launched via the service System.他们必须通过服务系统注册和启动的方式。 The need to manually attach the debugger.需要手动附加调试器。 The part where they can not display a UI since Vista . 自 Vista 以来,他们无法显示 UI的部分。

Some people have taken to doing the Service debugging and Development in a console application.有些人已经开始在控制台应用程序中进行服务调试和开发。 Console has no bagage that makes simulating the Service environment hard, and you got a way to output debug messages.控制台没有使模拟服务环境变得困难的包袱,并且您有一种输出调试消息的方法。 Of course you could also get away from Services entirely, using something like the TaskSheduler.当然,您也可以完全摆脱服务,使用类似 TaskSheduler 的东西。 It has all of the powers and then some, but none of the issues.它拥有所有的权力,然后是一些权力,但没有任何问题。 Even Microsoft has transitioned stuff out of services over into that.甚至微软也将服务中的东西转移到了服务中。

For this package, a windows service dosent appear to work as the "Server" side of the code, but having the winforms app as the server and the service as the client will work.对于这个包,windows 服务 dosent 似乎作为代码的“服务器”端工作,但将 winforms 应用程序作为服务器,将服务作为客户端工作。

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

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