.NET實現(xiàn)可交互的WINDOWS服務的實例代碼
這幾天想做個文件監(jiān)控服務,看了一下網上的關于WINDOWS服務的文章,數量都不少,都只講了如何做一個最基本的服務,卻沒有講述如何與用戶進行交互。查看了MSDN,看一下關于服務的描述:
Windows 服務應用程序在不同于登錄用戶的交互區(qū)域的窗口區(qū)域中運行。窗口區(qū)域是包含剪貼板、一組全局原子和一組桌面對象的安全對象。由于 Windows 服務的區(qū)域不是交互區(qū)域,因此 Windows 服務應用程序中引發(fā)的對話框將是不可見的,并且可能導致程序停止響應。同樣,錯誤信息應記錄在 Windows 事件日志中,而不是在用戶界面中引發(fā)。
.NET Framework 支持的 Windows 服務類不支持與交互區(qū)域(即登錄用戶)進行交互。同時,.NET Framework 不包含表示區(qū)域和桌面的類。如果 Windows 服務必須與其他區(qū)域進行交互,則需要訪問非托管的 Windows API。
也就是說我們要實現(xiàn)可交互的服務(比如我們想給服務在運行時做一些參數設置等),那我們一定要using System.Runtime.InteropServices
那么來看一下如果才能實現(xiàn)一個可交互的服務呢。步驟與實現(xiàn)基本的服務一樣(各位可自行參考MSDN或網上google一下).
在實現(xiàn)OnStart時要注意,這里可不能彈出一個FORM什么的。這樣做是沒有任何反應的。我們可以在這個方法里運行一個線程。該線程需要訪問窗口區(qū)域對象或桌面對象,當然 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
{
publicclass Service1 : System.ServiceProcess.ServiceBase
{
///
/// 必需的設計器變量。
///
private System.ComponentModel.Container components =null;
Thread threadForm =null;
public Service1()
{
// 該調用是 Windows.Forms 組件設計器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 調用后添加任何初始化
}
#region 組件設計器生成的代碼
///
/// 設計器支持所需的方法 - 不要使用代碼編輯器
/// 修改此方法的內容。
///
privatevoid InitializeComponent()
{
//
// Service1
//
this.ServiceName ="JadeWatchService";
}
#endregion
[STAThread]
staticvoid Main()
{
System.ServiceProcess.ServiceBase.Run(new Service1());
}
///
/// 清理所有正在使用的資源。
///
protectedoverridevoid Dispose(bool disposing)
{
if (disposing)
{
if (components !=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
///
/// 設置具體的操作,以便服務可以執(zhí)行它的工作。
///
protectedoverridevoid OnStart(string[] args)
{
threadForm =new Thread(new ThreadStart(FormShow));
threadForm.Start();
}
///
/// 停止此服務。
///
protectedoverridevoid 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")]
staticexternint GetDesktopWindow();
[DllImport("user32.dll")]
staticextern IntPtr GetProcessWindowStation();
[DllImport("kernel32.dll")]
staticextern IntPtr GetCurrentThreadId();
[DllImport("user32.dll")]
staticextern IntPtr GetThreadDesktop(IntPtr dwThread);
[DllImport("user32.dll")]
staticextern IntPtr OpenWindowStation(string a, bool b, int c);
[DllImport("user32.dll")]
staticextern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags,
bool fInherit, uint dwDesiredAccess);
[DllImport("user32.dll")]
staticextern IntPtr CloseDesktop(IntPtr p);
[DllImport("rpcrt4.dll", SetLastError =true)]
staticextern IntPtr RpcImpersonateClient(int i);
[DllImport("rpcrt4.dll", SetLastError =true)]
staticextern IntPtr RpcRevertToSelf();
[DllImport("user32.dll")]
staticextern IntPtr SetThreadDesktop(IntPtr a);
[DllImport("user32.dll")]
staticextern IntPtr SetProcessWindowStation(IntPtr a);
[DllImport("user32.dll")]
staticextern IntPtr CloseWindowStation(IntPtr a);
}
}
相關文章
asp.net后臺如何輸出js腳本使用什么方法可以實現(xiàn)
asp.net后臺如何輸出js腳本,用page.ClientScript.RegisterStartupScript方式實現(xiàn),實現(xiàn)示例如下,感興趣的朋友不要錯過2014-01-01ASP.NET Core使用JWT自定義角色并實現(xiàn)策略授權需要的接口
這篇文章介紹了ASP.NET Core使用JWT自定義角色并實現(xiàn)策略授權需要的接口,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-01-01如何在ASP.NET Core中使用Session的示例代碼
這篇文章主要介紹了如何在ASP.NET Core中使用Session的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01關于.NET/C#/WCF/WPF 打造IP網絡智能視頻監(jiān)控系統(tǒng)的介紹
本篇文章小編將為大家介紹,關于.NET/C#/WCF/WPF 打造IP網絡智能視頻監(jiān)控系統(tǒng)的介紹。需要的朋友參考下2013-04-04asp.net中Post表單保存頁面狀態(tài)并輸出源碼的實現(xiàn)方法
先執(zhí)行腳本,復制源碼到隱藏域里,再輸出源碼,注意代碼紅色設置2012-08-08ASP.NET在MVC中MaxLength特性設置無效的解決方法
這篇文章主要介紹了ASP.NET在MVC中MaxLength特性設置無效的解決方法,涉及對MVC中表單元素屬性的應用技巧,需要的朋友可以參考下2014-11-11asp.net類庫中添加WebService引用出現(xiàn)問題解決方法
在Web項目內添加WebService的引用是件很簡單的事情,不過對于一些新手朋友來說,就沒有那么簡單了,因為在添加的過程中總會遇到一些困難,接下來詳細介紹如何解決,感興趣的你可不要錯過了啊2013-02-02