最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

  這幾天想做個文件監(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);
    }
}

相關文章

最新評論

宁国市| 海南省| 贡觉县| 保山市| 孟州市| 多伦县| 平顶山市| 霍邱县| 丽水市| 鱼台县| 沂南县| 廊坊市| 荔波县| 大余县| 繁峙县| 武强县| 沛县| 冷水江市| 霍州市| 平邑县| 普宁市| 南和县| 海安县| 天等县| 泸州市| 马关县| 丽江市| 韶关市| 汝阳县| 老河口市| 大悟县| 三门县| 青龙| 化隆| 陇南市| 巫溪县| 上高县| 蛟河市| 扶风县| 汕尾市| 安平县|