这几天想做个文件监控服务,看了一下网上的关于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
{
    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);
        }
        ///
        /// 设置具体的操作,以便服务可以执行它的工作。
        ///
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);
    }
}
WINDOWS服务,可交互
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
 - 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
 - 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
 - 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
 - 群星《2024好听新歌42》AI调整音效【WAV分轨】
 - 王思雨-《思念陪着鸿雁飞》WAV
 - 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
 - 李健《无时无刻》[WAV+CUE][590M]
 - 陈奕迅《酝酿》[WAV分轨][502M]
 - 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
 - 群星《吉他王(黑胶CD)》[WAV+CUE]
 - 齐秦《穿乐(穿越)》[WAV+CUE]
 - 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
 - 邝美云《邝美云精装歌集》[DSF][1.6G]
 - 吕方《爱一回伤一回》[WAV+CUE][454M]