繁体   English   中英

Windows服务与桌面GUI应用程序的结构

[英]Structure of windows service with desktop GUI application

好的,所以我创建了一个Visual Studio 2013解决方案,其中包含两个C#项目,一个Win Form应用程序和一个Windows服务。 我正在使用高级安装程序11.6来创建我的安装包,我专门使用了内置的服务安装程序功能,该功能似乎可以正常工作。

获胜表单上有一些按钮,当我单击该按钮时,它将调用一个方法并传递一个数字作为参数(该数字对应于该服务的命令)。 然后,该方法使用管道将此参数发送到Windows服务。 我希望Windows服务始终侦听这些命令...并且当它接收到命令代码时,它将启动该操作。

问题是我肯定没有正确构造我的服务,并且我可能不正确地使用管道,因此我需要一些建议。 我不确定目前我的服务只是像脚本一样运行一次,尽管我不确定。 似乎来自win Forms应用程序的命令不仅在尝试将数据发送到服务,而且似乎正在尝试启动服务的新实例(获取有关无法从Windows XP启动服务的错误。命令行或调试器...,但不应该*从win表格中启动任何内容)。

无论如何,这里是win表单中用于将数据发送到服务的方法的代码。

胜出表格法

private void pipe_server(byte _command)
    {
        //create streams
        var sender = new AnonymousPipeServerStream(PipeDirection.Out,   HandleInheritability.Inheritable);

        //start client, pass pipe ids as command line parameter 
        string clientPath = @"C:\\test\\My_Service.exe";
        string senderID = sender.GetClientHandleAsString();

        var startInfo = new ProcessStartInfo(clientPath, senderID);
        startInfo.UseShellExecute = false;
        Process clientProcess = Process.Start(startInfo);

        //release resources handlet by client
        sender.DisposeLocalCopyOfClientHandle();

        //write data
        sender.WriteByte(_command);
    }

这是服务。

public partial class My_Service : ServiceBase
{
    public My_Service()
    {
        InitializeComponent();
    }

    static void Main(string[] args)
    {
        System.ServiceProcess.ServiceBase[] ServicesToRun;

        //Change the following line to match. 
        ServicesToRun = new
            System.ServiceProcess.ServiceBase[] { My_Service() };
        System.ServiceProcess.ServiceBase.Run(ServicesToRun);

        pipe_client(args);
    }

    //method that listens for commands from the GUI and parses them
    static void pipe_client(string[] args)
    {
        string parentSenderID;

        //get pipe handle id
        parentSenderID = args[0];

        //create streams
        var receiver = new AnonymousPipeClientStream(PipeDirection.In, parentSenderID);

        //read data
        int dataReceive = receiver.ReadByte();

        //parse commands
        if (dataReceive == 1)
        {
            do_method_1();
        }

        else if (dataReceive == 2)
        {
            do_method_2();
        }

        else if (dataReceive == 3)
        {
            do_method_3();
        }
    }
}

此处的示例将帮助您实现所需的工作: MSDN

看起来您需要做的就是设置某种形式的循环以轮询流以查找特定命令。 提供的示例显示了如何设置StreamReader和do循环,该循环检查从StreamReader.ReadLine()命令提取的字符串,直到识别出某些内容为止。

//method that listens for commands from the GUI and parses them
static void pipe_client(string[] args)
{
    string parentSenderID;

    //get pipe handle id
    parentSenderID = args[0];

    //create streams
    var receiver = new AnonymousPipeClientStream(PipeDirection.In, parentSenderID);

    using (StreamReader sr = new StreamReader(receiver))
    {
                // Display the read text to the console 
                string temp;

                // wait for message from server.
                do
                {
                    Console.WriteLine("waiting for message...");
                    temp = sr.ReadLine();
                }
                while (!<temp in some check variable or array>);

                switch(temp)
                { 
                   handle everything here.  
                }
    }
}

暂无
暂无

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

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