欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.NET實現(xiàn)可交互的WINDOWS服務(wù)的實例代碼

 更新時間:2013年03月01日 14:13:34   作者:  
那么來看一下如何才能實現(xiàn)一個可交互的服務(wù)呢。步驟與實現(xiàn)基本的服務(wù)一樣

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

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

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

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

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

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

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

復(fù)制代碼 代碼如下:

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
    {
        ///
        /// 必需的設(shè)計器變量。
        ///
private System.ComponentModel.Container components =null;
        Thread threadForm =null;
        public Service1()
        {
            // 該調(diào)用是 Windows.Forms 組件設(shè)計器所必需的。
            InitializeComponent();

            // TODO: 在 InitComponent 調(diào)用后添加任何初始化
        }

        #region 組件設(shè)計器生成的代碼
        ///
        /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器
        /// 修改此方法的內(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);
        }

        ///
        /// 設(shè)置具體的操作,以便服務(wù)可以執(zhí)行它的工作。
        ///
protectedoverridevoid OnStart(string[] args)
        {
            threadForm =new Thread(new ThreadStart(FormShow));
            threadForm.Start();
        }

        ///
        /// 停止此服務(wù)。
        ///
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,可以顯示在托盤里,用戶可點擊托盤圖標(biāo)進(jìn)行設(shè)置
            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);
    }
}

相關(guān)文章

最新評論