使用命名管道在winform程序中输出windows service运行状态
2011-08-25 15:21
429 查看
最近的工作需要写一个windows服务,定时执行任务。众所周知windows service程序是没有用户界面的,而有时候是需要知道服务正在做什么的。怎么办呢?可否用winform程序显示服务执行状态呢?如何让winform取到windows service进程的消息呢?这个场景用命名管道不是正合适吗?是的,命名管道正合适。
先来想下实现的思路:
Windows服务程序要声明NamedPipeServerStream对象实例,然后等待winform程序的NamePipeClientStream实例连接到管道;连接到之后windows服务程序将其执行状态通过命名管道输出给winforms程序。
Winform程序可以随时断开管道连接,断开之后windows服务程序要继续等待winform程序再次连接到管道,连接之后可以继续输出其执行状态。另外winform程序可以在局域网的任何一台机器上连接到服务器上的windows服务程序,以方便观察服务的执行状态
思路就是这个样子了,下面我们先看下Windows服务程序中的关键实现:
Winform程序中关键实现部分:
源码下载
前一段时间写过两篇命名管道方面的文章:
使用管道在进程间通信 (System.IO.Pipes使用)
System.IO系列:局域网内多线程使用命名管道在进程之间通信实例
先来想下实现的思路:
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系列:局域网内多线程使用命名管道在进程之间通信实例
相关文章推荐
- 使用命名管道在winform程序中输出windows service运行状态
- Windows Service开发日志四(用程序设置服务的运行状态及启动方式)
- 使用Java Service Wrapper以windows服务的形式运行Java程序
- 如何在运行时切换WinForm程序使用的语言?
- Windows 防火墙无法运行 "由于另一个程序或正在运行的服务可能正在使用网络地址转换组件"
- Windows下使用Sublime Text 2/3调试/运行 C/C++ 程序(可输入数据版本)
- HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo
- 使用TextTest来做认定测试——本质是通过diff对比程序的运行log输出,来看测试结果和预期结果是否相同
- 使用进程互斥实现WinForm程序只运行一个实例(VB.Net)
- Windows下使用Sublime Text 3调试/运行 C/C++ 程序
- 使用.bat文件在Windows下运行Java程序时怎样避免DOS窗口
- 以Windows Service的方式运行Python程序
- 在Windows 7 下使用Visual Studio 2010 编写自动申请管理员权限运行的程序
- 使用Java Service Wrapper 把Java程序作为Windows系统服务
- 除非 Windows Activation Service (WAS)和万维网发布服务(W3SVC)均处于运行状态,否则无法启动网站。IIS 7
- windows下使用重叠IO(OVERLAPPE)命名管道服务端
- 运行Dos命令并得到dos的输出文本(使用管道函数CreatePipe和PeekNamedPipe)
- Windows 下使用 GNUstep 编译并运行 Objective-C 程序
- 使用JConsole监控java程序的运行状态总结
- 使用Java Service Wrapper 把Java程序作为Windows系统服务