使用命名管道在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系列:局域网内多线程使用命名管道在进程之间通信实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值