.NET實現(xiàn)可交互的WINDOWS服務的實例代碼
這幾天想做個文件監(jiān)控服務,看了一下網(wǎng)上的關于WINDOWS服務的文章,數(shù)量都不少,都只講了如何做一個最基本的服務,卻沒有講述如何與用戶進行交互。查看了MSDN,看一下關于服務的描述:
Windows 服務應用程序在不同于登錄用戶的交互區(qū)域的窗口區(qū)域中運行。窗口區(qū)域是包含剪貼板、一組全局原子和一組桌面對象的安全對象。由于 Windows 服務的區(qū)域不是交互區(qū)域,因此 Windows 服務應用程序中引發(fā)的對話框?qū)⑹遣豢梢姷模⑶铱赡軐е鲁绦蛲V鬼憫?。同樣,錯誤信息應記錄在 Windows 事件日志中,而不是在用戶界面中引發(fā)。
.NET Framework 支持的 Windows 服務類不支持與交互區(qū)域(即登錄用戶)進行交互。同時,.NET Framework 不包含表示區(qū)域和桌面的類。如果 Windows 服務必須與其他區(qū)域進行交互,則需要訪問非托管的 Windows API。
也就是說我們要實現(xiàn)可交互的服務(比如我們想給服務在運行時做一些參數(shù)設置等),那我們一定要using System.Runtime.InteropServices
那么來看一下如果才能實現(xiàn)一個可交互的服務呢。步驟與實現(xiàn)基本的服務一樣(各位可自行參考MSDN或網(wǎng)上google一下).
在實現(xiàn)OnStart時要注意,這里可不能彈出一個FORM什么的。這樣做是沒有任何反應的。我們可以在這個方法里運行一個線程。該線程需要訪問窗口區(qū)域?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()
{
// 該調(diào)用是 Windows.Forms 組件設計器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 調(diào)用后添加任何初始化
}
#region 組件設計器生成的代碼
///
/// 設計器支持所需的方法 - 不要使用代碼編輯器
/// 修改此方法的內(nèi)容。
///
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 Core應用來發(fā)布靜態(tài)文件
本文主要通過一些簡單的實例來體驗一下如何在一個ASP.NET Core應用中發(fā)布靜態(tài)文件。針對不同格式的靜態(tài)文件請求的處理,ASP.NET Core為我們提供了三個中間件,它們將是本系列文章論述的重點。有需要的朋友可以看下2016-12-12
Visual Studio 2017通過SSH調(diào)試Linux上.NET Core
這篇文章主要為大家詳細介紹了Visual Studio 2017通過SSH調(diào)試Linux 上.NET Core的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
asp.net 1.1/ 2.0 中快速實現(xiàn)單點登陸
asp.net 1.1/ 2.0 中快速實現(xiàn)單點登陸...2007-04-04
.Net6集成IdentityServer4?+AspNetCore?Identity讀取數(shù)據(jù)表用戶且鑒權(quán)授權(quán)管理A
這篇文章主要介紹了.Net6集成IdentityServer4與AspNetCore?Identity讀取數(shù)據(jù)表用戶且鑒權(quán)授權(quán)管理API,IdentityServer4?實現(xiàn)鑒權(quán)、授權(quán),AspNetCore?Identity實現(xiàn)數(shù)據(jù)庫用戶管理表直接生成,下文詳情需要朋友可以參考一下2022-07-07
asp.net 從客戶端中檢測到有潛在危險的 Request.Form 值錯誤解
asp.net程序運行時出現(xiàn)以下錯誤: “/news”應用程序中的服務器錯誤。2009-05-05

