在Windows服务和表单应用程序之间发送和接收消息的最佳方法 [英] Best way to send and receive messages between a Windows service and form application

查看:158
本文介绍了在Windows服务和表单应用程序之间发送和接收消息的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现IPC,但是我所做的与我见过的文档有所不同。



我需要异步发送,接收,并用服务和表单应用程序响应。

  // Service1.cs 
serverPipe = new NamedPipeServerStream(@ testpipe,PipeDirection.InOut,
1,PipeTransmissionMode.Message,PipeOptions.Asynchronous);

//LoginForm.cs
clientPipe = new NamedPipeClientStream(@。,@testpipe,PipeDirection.InOut
PipeOptions.Asynchronous);

我相信这会设定双方。



从一个写到另一个:

  //在Service1.cs和Login.cs中
private void pipeWriter()
{
StreamWriter write = null;
write = new StreamWriter(clientPipe);

if(clientPipe.IsConnected)
{
write.Write(clientPipe);
write.Flush();
}
}

...并阅读:

  //在Service1.cs和Login.cs中
private void pipeReader()
{
try
{
while(true)
{
using(StreamReader sr = new StreamReader(clientPipe))
{
string message; ((message = sr.ReadLine())!= null)

//读取并响应写入流的消息
}



catch(Exception ex)
{

}
}

我是否正确直接?在Windows服务和表单应用程序之间进行通信还有另外一种(更好的)方式吗?

解决方案

我想你应该看看WCF - Windows Communication Foundation,你定义契约(服务契约,数据契约),发布服务和使用服务,而不用担心协议,因为你可以通过配置服务器和客户端应用程序来选择它。

您可以查看以下链接以获取信息: http://www.codeproject.com/Articles/29243 / A-Windows-Communication-Foundation-WCF-Overview

  • http://msdn.microsoft.com/en-us/library/ee958158.aspx



  • 希望它有帮助,
    干杯


    I'm attempting to implement IPC, but what I'm doing is somewhat different from the documentation I've seen.

    I need to asynchronously send, receive, and respond with a service and form application.

    //Service1.cs
    serverPipe = new NamedPipeServerStream(@"testpipe", PipeDirection.InOut, 
       1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
    
    //LoginForm.cs
    clientPipe = new NamedPipeClientStream(@".", @"testpipe", PipeDirection.InOut
        PipeOptions.Asynchronous);
    

    I believe this sets up both sides.

    To write from one to the other:

    //In Service1.cs and Login.cs
    private void pipeWriter()
    {
        StreamWriter write = null;
        write = new StreamWriter(clientPipe);
    
        if (clientPipe.IsConnected)
        {
            write.Write(clientPipe);
            write.Flush();
        }
    }
    

    ... and to read:

    //In Service1.cs and Login.cs
    private void pipeReader()
    {
        try
        {
            while (true)
            {
                using (StreamReader sr = new StreamReader(clientPipe))
                {
                    string message;
    
                    while ((message = sr.ReadLine()) != null)
                    {
                        //read and respond to messages written to stream
                    }
                }
            }
        }
        catch (Exception ex)
        {
    
        }
    }
    

    Am I in the right direct? Is there another (better) way to communicate between a Windows service and a form application?

    解决方案

    since you're in .NET I guess you should look into WCF - Windows Communication Foundation, you define the contracts (Service contract, data contracts), publish the service and use the service without worrying about the protocol since you can choose it by configuring the server and the client app.

    You can take a look at these links for some in information:

    Hope it helps, Cheers

    这篇关于在Windows服务和表单应用程序之间发送和接收消息的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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