.NET實現可交互的WINDOWS服務
這幾天想做個文件監控服務,看了一下網上的關于WINDOWS服務的文章,數量都不少,都只講了如何做一個最基本的服務,卻沒有講述如何與用戶進行交互。查看了MSDN,看一下關于服務的描述:
Windows 服務應用程序在不同于登錄用戶的交互區域的窗口區域中運行。窗口區域是包含剪貼板、一組全局原子和一組桌面對象的安全對象。由于 Windows 服務的區域不是交互區域,因此 Windows 服務應用程序中引發的對話框將是不可見的,并且可能導致程序停止響應。同樣,錯誤信息應記錄在 Windows 事件日志中,而不是在用戶界面中引發。
.NET Framework 支持的 Windows 服務類不支持與交互區域(即登錄用戶)進行交互。同時,.NET Framework 不包含表示區域和桌面的類。如果 Windows 服務必須與其他區域進行交互,則需要訪問非托管的 Windows API。
也就是說我們要實現可交互的服務(比如我們想給服務在運行時做一些參數設置等),那我們一定要using System.Runtime.InteropServices
那么來看一下如果才能實現一個可交互的服務呢。步驟與實現基本的服務一樣(各位可自行參考MSDN或網上google一下).
在實現OnStart時要注意,這里可不能彈出一個FORM什么的。這樣做是沒有任何反應的。我們可以在這個方法里運行一個線程。該線程需要訪問窗口區域對象或桌面對象,當然 framework里是沒有提供這些的,要訪問非托管代碼的。
來看一下代碼,再運行試一下。
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Threading; using System.Runtime.InteropServices; namespace FileWatchService { public class Service1 : System.ServiceProcess.ServiceBase { /// /// 必需的設計器變量。 /// private System.ComponentModel.Container components = null; Thread threadForm = null; public Service1() { // 該調用是 Windows.Forms 組件設計器所必需的。 InitializeComponent(); // TODO: 在 InitComponent 調用后添加任何初始化 } #region 組件設計器生成的代碼 /// /// 設計器支持所需的方法 - 不要使用代碼編輯器 /// 修改此方法的內容。 /// private void InitializeComponent() { // // Service1 // this.ServiceName = "JadeWatchService"; } #endregion [STAThread] static void Main() { System.ServiceProcess.ServiceBase.Run(new Service1()); } /// /// 清理所有正在使用的資源。 /// protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } /// /// 設置具體的操作,以便服務可以執行它的工作。 /// protected override void OnStart(string[] args) { threadForm = new Thread(new ThreadStart(FormShow)); threadForm.Start(); } /// /// 停止此服務。 /// protected override void OnStop() { if (threadForm != null) { if (threadForm.IsAlive) { threadForm.Abort(); threadForm = null; } } } void FormShow() { GetDesktopWindow(); IntPtr hwinstaSave = GetProcessWindowStation(); IntPtr dwThreadId = GetCurrentThreadId(); IntPtr hdeskSave = GetThreadDesktop(dwThreadId); IntPtr hwinstaUser = OpenWindowStation("WinSta0", false, 33554432); if (hwinstaUser == IntPtr.Zero) { RpcRevertToSelf(); return; } SetProcessWindowStation(hwinstaUser); IntPtr hdeskUser = OpenDesktop("Default", 0, false, 33554432); RpcRevertToSelf(); if (hdeskUser == IntPtr.Zero) { SetProcessWindowStation(hwinstaSave); CloseWindowStation(hwinstaUser); return; } SetThreadDesktop(hdeskUser); IntPtr dwGuiThreadId = dwThreadId; Form1 f = new Form1(); //此FORM1可以帶notifyIcon,可以顯示在托盤里,用戶可點擊托盤圖標進行設置 System.Windows.Forms.Application.Run(f); dwGuiThreadId = IntPtr.Zero; SetThreadDesktop(hdeskSave); SetProcessWindowStation(hwinstaSave); CloseDesktop(hdeskUser); CloseWindowStation(hwinstaUser); } [DllImport("user32.dll")] static extern int GetDesktopWindow(); [DllImport("user32.dll")] static extern IntPtr GetProcessWindowStation(); [DllImport("kernel32.dll")] static extern IntPtr GetCurrentThreadId(); [DllImport("user32.dll")] static extern IntPtr GetThreadDesktop(IntPtr dwThread); [DllImport("user32.dll")] static extern IntPtr OpenWindowStation(string a, bool b, int c); [DllImport("user32.dll")] static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags, bool fInherit, uint dwDesiredAccess); [DllImport("user32.dll")] static extern IntPtr CloseDesktop(IntPtr p); [DllImport("rpcrt4.dll", SetLastError = true)] static extern IntPtr RpcImpersonateClient(int i); [DllImport("rpcrt4.dll", SetLastError = true)] static extern IntPtr RpcRevertToSelf(); [DllImport("user32.dll")] static extern IntPtr SetThreadDesktop(IntPtr a); [DllImport("user32.dll")] static extern IntPtr SetProcessWindowStation(IntPtr a); [DllImport("user32.dll")] static extern IntPtr CloseWindowStation(IntPtr a); } }