使用命名管道在winform程序中输出windows service运行状态

最近的工作需要写一个windows服务,定时执行任务。众所周知windows service程序是没有用户界面的,而有时候是需要知道服务正在做什么的。怎么办呢?可否用winform程序显示服务执行状态呢?如何让winform取到windows service进程的消息呢?这个场景用命名管道不是正合适吗?是的,命名管道正合适。

先来想下实现的思路:

Windows服务程序要声明NamedPipeServerStream对象实例,然后等待winform程序的NamePipeClientStream实例连接到管道;连接到之后windows服务程序将其执行状态通过命名管道输出给winforms程序。

Winform程序可以随时断开管道连接,断开之后windows服务程序要继续等待winform程序再次连接到管道,连接之后可以继续输出其执行状态。另外winform程序可以在局域网的任何一台机器上连接到服务器上的windows服务程序,以方便观察服务的执行状态

思路就是这个样子了,下面我们先看下Windows服务程序中的关键实现:

public class ServiceStatusPublishManager
{
    private const int MAX_LENGTH = 200;

    private List<string> _msgs;

    private ManagerImple _managerImp;

    protected ServiceStatusPublishManager()
    {
        _managerImp = new ManagerImple(this);
        _msgs = new List<string>();
    }

    static public readonly ServiceStatusPublishManager Instance = new ServiceStatusPublishManager();

    public void AppendMessage(string msg)
    {
        lock (this) _msgs.Add(msg);

        if (_msgs.Count > MAX_LENGTH)
        {
            lock (this)
            {
                _msgs.RemoveAt(0);
                if (_managerImp._msgIndex == _msgs.Count) _managerImp._msgIndex -= 2;
            }
        }
    }


    class ManagerImple
    {
        private ServiceStatusPublishManager _trace;

        private Thread _thread;

        public ManagerImple(ServiceStatusPublishManager trace)
        {
            _trace = trace;
            _thread = new Thread(WriteServiceStatus);
            _thread.Start();
        }

        public int _msgIndex = 0;

        private void WriteServiceStatus(object stats)
        {
            while (true)
            {
                try
                {
                    using (NamedPipeServerStream serverStream = new NamedPipeServerStream("ServiceStatusPipe", PipeDirection.InOut, 10, PipeTransmissionMode.Message))
                    {
                        serverStream.WaitForConnection();

                        while (serverStream.IsConnected)
                        {
                            bool writeSomeMessage = false;
                            while (_msgIndex < _trace._msgs.Count)
                            {
                                string msg = _trace._msgs[_msgIndex];
                                byte[] bytes = Encoding.UTF8.GetBytes(msg);
                                serverStream.Write(bytes, 0, bytes.Length);
                                serverStream.Flush();
                                _msgIndex++;
                                writeSomeMessage = true;
                            }

                            if (!writeSomeMessage)
                            {
                                byte[] bytes = Encoding.UTF8.GetBytes("无消息输出,MsgIndex = " + _msgIndex + ",消息总数 = " + _trace._msgs.Count);

                                serverStream.Write(bytes, 0, bytes.Length);
                                serverStream.Flush();
                                Thread.Sleep(1000 * 10);
                            }
                            Thread.Sleep(1000);
                        }

                    }
                }
                catch (Exception ex)
                {
                }
            }

        }
    }
}

Winform程序中关键实现部分:

void RefreshStatus(object stats)
{
    try
    {
        using (System.IO.Pipes.NamedPipeClientStream stream = new System.IO.Pipes.NamedPipeClientStream(ServerName, "ServiceStatusPipe"))
        {
            stream.Connect(3000);
            stream.ReadMode = System.IO.Pipes.PipeTransmissionMode.Message;

            byte[] bytes = new byte[16];
            char[] chars = new char[16];
            int read;
            do
            {
                string msg = string.Empty;

                do
                {
                    read = stream.Read(bytes, 0, bytes.Length);
                    if (read > 0)
                    {
                        int getCharCount = _decoder.GetChars(bytes, 0, read, chars, 0);
                        msg += new string(chars, 0, getCharCount);
                    }
                } while (!stream.IsMessageComplete);


                if (!string.IsNullOrEmpty(msg))
                {
                    AddMsg(msg);
                }

                _decoder.Reset();

                Thread.Sleep(20);
            } while (!_closing);
        }
    }
    catch (Exception ex)
    {
        AddMsg("异常:" + ex.Message);
        Thread.Sleep(1000);

        RefreshStatus(null);
    }
}

源码下载

前一段时间写过两篇命名管道方面的文章:

使用管道在进程间通信 (System.IO.Pipes使用)

System.IO系列:局域网内多线程使用命名管道在进程之间通信实例

posted @ 2011-08-25 15:21  玉开  阅读(4549)  评论(2编辑  收藏  举报