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

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

先来想下实现的思路:

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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程序中关键实现部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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 @   玉开  阅读(4566)  评论(2编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2010-08-25 w3wp进程发生死锁ISAPI '..\aspnet_isapi.dll' 报告它自身有问题,原因'Deadlock detected'
点击右上角即可分享
微信分享提示