自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#WINDOWS服務(wù)交互的實(shí)現(xiàn)

開(kāi)發(fā) 后端
C#WINDOWS服務(wù)交互的實(shí)現(xiàn)是如何辦到的呢?C#WINDOWS服務(wù)交互的實(shí)現(xiàn)的具體實(shí)施是怎么樣子的呢?那么本文就向你介紹C#WINDOWS服務(wù)交互的實(shí)現(xiàn)的具體內(nèi)容。

C#WINDOWS服務(wù)交互的實(shí)現(xiàn)的前言:這幾天想做個(gè)文件監(jiān)控服務(wù),看了一下網(wǎng)上的關(guān)于WINDOWS服務(wù)的文章,數(shù)量都不少,都只講了如何做一個(gè)最基本的服務(wù),卻沒(méi)有講述如何與用戶(hù)進(jìn)行交互。查看了MSDN,看一下關(guān)于服務(wù)的描述:

WINDOWS服務(wù)交互應(yīng)用程序在不同于登錄用戶(hù)的交互區(qū)域的窗口區(qū)域中運(yùn)行。窗口區(qū)域是包含剪貼板、一組全局原子和一組桌面對(duì)象的安全對(duì)象。由于 WINDOWS服務(wù)交互的區(qū)域不是交互區(qū)域,因此 Windows 服務(wù)應(yīng)用程序中引發(fā)的對(duì)話框?qū)⑹遣豢梢?jiàn)的,并且可能導(dǎo)致程序停止響應(yīng)。同樣,錯(cuò)誤信息應(yīng)記錄在 Windows 事件日志中,而不是在用戶(hù)界面中引發(fā)。

 .NET Framework 支持的 WINDOWS服務(wù)交互類(lèi)不支持與交互區(qū)域(即登錄用戶(hù))進(jìn)行交互。同時(shí),.NET Framework 不包含表示區(qū)域和桌面的類(lèi)。如果 WINDOWS服務(wù)交互務(wù)必須與其他區(qū)域進(jìn)行交互,則需要訪問(wèn)非托管的 Windows API。

也就是說(shuō)我們要實(shí)現(xiàn)可交互的服務(wù)(比如我們想給服務(wù)在運(yùn)行時(shí)做一些參數(shù)設(shè)置等),那我們一定要using System.Runtime.InteropServices

那么來(lái)看一下如果才能實(shí)現(xiàn)一個(gè)可交互的服務(wù)呢。步驟與實(shí)現(xiàn)基本的服務(wù)一樣(各位可自行參考MSDN或網(wǎng)上google一下).

在實(shí)現(xiàn)OnStart時(shí)要注意,這里可不能彈出一個(gè)FORM什么的。這樣做是沒(méi)有任何反應(yīng)的。我們可以在這個(gè)方法里運(yùn)行一個(gè)線程。該線程需要訪問(wèn)窗口區(qū)域?qū)ο蠡蜃烂鎸?duì)象,當(dāng)然 framework里是沒(méi)有提供這些的,要訪問(wèn)非托管代碼的。

來(lái)看一下代碼,再運(yùn)行試一下。

  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. /// 必需的設(shè)計(jì)器變量。  
  15. ///   
  16. private System.ComponentModel.Container components = null;  
  17. Thread threadForm=null;  
  18. public Service1()  
  19. {  
  20. // 該調(diào)用是 Windows.Forms 組件設(shè)計(jì)器所必需的。  
  21. InitializeComponent();  
  22.  
  23. // TODO: 在 InitComponent 調(diào)用后添加任何初始化  
  24. }  
  25.  
  26. #region 組件設(shè)計(jì)器生成的代碼  
  27. ///   
  28. /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器   
  29. /// 修改此方法的內(nèi)容。  
  30. ///   
  31. private void InitializeComponent()  
  32. {  
  33. //   WINDOWS服務(wù)交互
  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服務(wù)交互
  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. /// 設(shè)置具體的操作,以便服務(wù)可以執(zhí)行它的工作。  
  63. ///   
  64. protected override void OnStart(string[] args)  
  65. {  
  66. threadForm=new Thread(new ThreadStart(FormShow));  
  67. threadForm.Start();  
  68. }  
  69.  
  70. ///   WINDOWS服務(wù)交互
  71. /// 停止此服務(wù)。  
  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,可以顯示在托盤(pán)里,用戶(hù)可點(diǎn)擊托盤(pán)圖標(biāo)進(jìn)行設(shè)置  
  115. System.Windows.Forms.Application.Run(f);  
  116.  //WINDOWS服務(wù)交互
  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服務(wù)交互的實(shí)現(xiàn)的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你學(xué)習(xí)和了解C#WINDOWS服務(wù)交互的實(shí)現(xiàn)有所幫助。

【編輯推薦】

  1. C#創(chuàng)建Windows服務(wù)學(xué)習(xí)的一點(diǎn)體會(huì)
  2. C#Windows服務(wù)程序之添加安裝程序圖解
  3. C#Windows服務(wù)程序開(kāi)發(fā)實(shí)例淺析
  4. C#Windows服務(wù)程序開(kāi)發(fā)淺析
  5. C#Windows服務(wù)程序的快速開(kāi)發(fā)
責(zé)任編輯:仲衡 來(lái)源: 百度空間
相關(guān)推薦

2009-08-14 10:50:09

Windows服務(wù)介紹

2009-08-14 15:54:50

Windows服務(wù)程序C#Windows服務(wù)

2009-08-14 14:25:09

Windows服務(wù)程序

2009-08-14 14:45:03

C#Windows服務(wù)

2009-08-14 15:19:38

Windows服務(wù)程序Windows服務(wù)

2009-08-14 15:06:08

Windows服務(wù)程序

2009-08-14 15:47:18

C#Windows服務(wù)

2009-08-14 11:15:19

文件監(jiān)視C#Windows服務(wù)

2009-08-14 10:42:16

Timer控件的使用C#windows服務(wù)

2009-08-14 16:13:25

C#windows服務(wù)

2009-08-14 16:48:39

C#Windows服務(wù)

2009-08-14 14:17:16

C#Windows服務(wù)

2009-08-14 16:24:00

Windows服務(wù)程序

2009-08-14 18:04:59

C#Windows應(yīng)用

2009-08-14 13:41:13

C#Windows服務(wù)

2009-08-14 17:27:30

C#Windows應(yīng)用

2009-08-14 17:36:20

C#Windows應(yīng)用

2009-08-14 17:55:52

C#Windows應(yīng)用

2009-08-14 17:43:20

C#Windows應(yīng)用

2009-08-14 18:00:22

C#Windows應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)