C#WINDOWS服务交互的实现

开发 后端
C#WINDOWS服务交互的实现是如何办到的呢?C#WINDOWS服务交互的实现的具体实施是怎么样子的呢?那么本文就向你介绍C#WINDOWS服务交互的实现的具体内容。

C#WINDOWS服务交互的实现的前言:这几天想做个文件监控服务,看了一下网上的关于WINDOWS服务的文章,数量都不少,都只讲了如何做一个最基本的服务,却没有讲述如何与用户进行交互。查看了MSDN,看一下关于服务的描述:

WINDOWS服务交互应用程序在不同于登录用户的交互区域的窗口区域中运行。窗口区域是包含剪贴板、一组全局原子和一组桌面对象的安全对象。由于 WINDOWS服务交互的区域不是交互区域,因此 Windows 服务应用程序中引发的对话框将是不可见的,并且可能导致程序停止响应。同样,错误信息应记录在 Windows 事件日志中,而不是在用户界面中引发。

 .NET Framework 支持的 WINDOWS服务交互类不支持与交互区域(即登录用户)进行交互。同时,.NET Framework 不包含表示区域和桌面的类。如果 WINDOWS服务交互务必须与其他区域进行交互,则需要访问非托管的 Windows API。

也就是说我们要实现可交互的服务(比如我们想给服务在运行时做一些参数设置等),那我们一定要using System.Runtime.InteropServices

那么来看一下如果才能实现一个可交互的服务呢。步骤与实现基本的服务一样(各位可自行参考MSDN或网上google一下).

在实现OnStart时要注意,这里可不能弹出一个FORM什么的。这样做是没有任何反应的。我们可以在这个方法里运行一个线程。该线程需要访问窗口区域对象或桌面对象,当然 framework里是没有提供这些的,要访问非托管代码的。

来看一下代码,再运行试一下。

  1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Diagnostics;  
  6. using System.ServiceProcess;  
  7. using System.Threading;  
  8. using System.Runtime.InteropServices;  
  9. namespace FileWatchService  
  10. {  
  11. public class Service1 : System.ServiceProcess.ServiceBase  
  12. {  
  13. ///   
  14. /// 必需的设计器变量。  
  15. ///   
  16. private System.ComponentModel.Container components = null;  
  17. Thread threadForm=null;  
  18. public Service1()  
  19. {  
  20. // 该调用是 Windows.Forms 组件设计器所必需的。  
  21. InitializeComponent();  
  22.  
  23. // TODO: 在 InitComponent 调用后添加任何初始化  
  24. }  
  25.  
  26. #region 组件设计器生成的代码  
  27. ///   
  28. /// 设计器支持所需的方法 - 不要使用代码编辑器   
  29. /// 修改此方法的内容。  
  30. ///   
  31. private void InitializeComponent()  
  32. {  
  33. //   WINDOWS服务交互
  34. // Service1  
  35. //   
  36. this.ServiceName = "JadeWatchService";  
  37.  
  38. }  
  39. #endregion  
  40. [STAThread]  
  41. static void Main()   
  42. {  
  43. System.ServiceProcess.ServiceBase.Run(new Service1());  
  44.  
  45. }  
  46. ///   WINDOWS服务交互
  47. /// 清理所有正在使用的资源。  
  48. ///   
  49. protected override void Dispose( bool disposing )   
  50. {  
  51. if( disposing )  
  52. {  
  53. if (components != null)   
  54. {  
  55. components.Dispose();  
  56. }  
  57. }  
  58. base.Dispose( disposing );  
  59. }  
  60.  
  61. ///   
  62. /// 设置具体的操作,以便服务可以执行它的工作。  
  63. ///   
  64. protected override void OnStart(string[] args)  
  65. {  
  66. threadForm=new Thread(new ThreadStart(FormShow));  
  67. threadForm.Start();  
  68. }  
  69.  
  70. ///   WINDOWS服务交互
  71. /// 停止此服务。  
  72. ///   
  73. protected override void OnStop()  
  74. {  
  75. if(threadForm!=null)  
  76. {  
  77. if(threadForm.IsAlive)  
  78. {  
  79. threadForm.Abort();  
  80. threadForm=null;  
  81. }  
  82. }  
  83. }  
  84.  
  85. void FormShow()  
  86. {  
  87.  
  88. GetDesktopWindow();   
  89. IntPtr hwinstaSave = GetProcessWindowStation();   
  90. IntPtr dwThreadId = GetCurrentThreadId();   
  91. IntPtr hdeskSave = GetThreadDesktop(dwThreadId);   
  92. IntPtr hwinstaUser = OpenWindowStation(
  93. "WinSta0"false,33554432);   
  94. if (hwinstaUser == IntPtr.Zero)   
  95. {   
  96. RpcRevertToSelf();   
  97. return ;  
  98. }   
  99. SetProcessWindowStation(hwinstaUser);   
  100. IntPtr hdeskUser = OpenDesktop(
  101. "Default", 0, false, 33554432);   
  102. RpcRevertToSelf();   
  103. if (hdeskUser == IntPtr.Zero)   
  104. {   
  105. SetProcessWindowStation(hwinstaSave);   
  106. CloseWindowStation(hwinstaUser);   
  107. return ;   
  108. }   
  109. SetThreadDesktop(hdeskUser);   
  110.  
  111. IntPtr dwGuiThreadId = dwThreadId;   
  112.  
  113. Form1 f=new Form1(); 
  114. //此FORM1可以带notifyIcon,可以显示在托盘里,用户可点击托盘图标进行设置  
  115. System.Windows.Forms.Application.Run(f);  
  116.  //WINDOWS服务交互
  117.  
  118. dwGuiThreadId = IntPtr.Zero;   
  119. SetThreadDesktop(hdeskSave);   
  120. SetProcessWindowStation(hwinstaSave);   
  121. CloseDesktop(hdeskUser);   
  122. CloseWindowStation(hwinstaUser);   
  123. }  
  124.  
  125. [DllImport("user32.dll")]  
  126. static extern int GetDesktopWindow();  
  127.  
  128. [DllImport("user32.dll")]  
  129. static extern IntPtr GetProcessWindowStation();  
  130.  
  131. [DllImport("kernel32.dll")]  
  132. static extern IntPtr GetCurrentThreadId();  
  133.  
  134. [DllImport("user32.dll")]  
  135. static extern IntPtr GetThreadDesktop(IntPtr dwThread);  
  136.  
  137. [DllImport("user32.dll")]  
  138. static extern IntPtr OpenWindowStation(string a,bool b,int c);  
  139.  
  140. [DllImport("user32.dll")]  
  141. static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags,  
  142. bool fInherit, uint dwDesiredAccess);  
  143.  
  144. [DllImport("user32.dll")]  
  145. static extern IntPtr CloseDesktop(IntPtr p);  
  146.  
  147. [DllImport("rpcrt4.dll", SetLastError=true)]  
  148. static extern IntPtr RpcImpersonateClient(int i);  
  149.  
  150.  
  151. [DllImport("rpcrt4.dll", SetLastError=true)]  
  152. static extern IntPtr RpcRevertToSelf();  
  153.  
  154. [DllImport("user32.dll")]  
  155. static extern IntPtr SetThreadDesktop(IntPtr a);  
  156.  
  157. [DllImport("user32.dll")]  
  158. static extern IntPtr SetProcessWindowStation(IntPtr a);  
  159. [DllImport("user32.dll")]  
  160. static extern IntPtr CloseWindowStation(IntPtr a);  
  161. }  

C#WINDOWS服务交互的实现的相关内容就向你介绍到这里,希望对你学习和了解C#WINDOWS服务交互的实现有所帮助。

【编辑推荐】

  1. C#创建Windows服务学习的一点体会
  2. C#Windows服务程序之添加安装程序图解
  3. C#Windows服务程序开发实例浅析
  4. C#Windows服务程序开发浅析
  5. C#Windows服务程序的快速开发
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-14 10:50:09

Windows服务介绍

2009-08-14 15:54:50

Windows服务程序C#Windows服务

2009-08-14 14:45:03

C#Windows服务

2009-08-14 14:25:09

Windows服务程序

2009-08-14 15:06:08

Windows服务程序

2009-08-14 15:47:18

C#Windows服务

2009-08-14 15:19:38

Windows服务程序Windows服务

2009-08-14 11:15:19

文件监视C#Windows服务

2009-08-14 10:42:16

Timer控件的使用C#windows服务

2009-08-14 14:17:16

C#Windows服务

2009-08-14 16:24:00

Windows服务程序

2009-08-14 16:13:25

C#windows服务

2009-08-14 16:48:39

C#Windows服务

2009-08-14 18:04:59

C#Windows应用

2009-08-14 13:41:13

C#Windows服务

2009-08-14 17:27:30

C#Windows应用

2009-08-14 17:36:20

C#Windows应用

2009-08-14 17:55:52

C#Windows应用

2009-08-14 17:43:20

C#Windows应用

2009-08-14 18:00:22

C#Windows应用
点赞
收藏

51CTO技术栈公众号