none
Windows Services问题 RRS feed

  • 问题

  • 我想用windows service 侦听用户解锁的事件,但是vista之后的windows版本,service都在session 0中,第一个登录的用户在session 1中,service侦听不到此事件。不知道大家有没解决此类问题的方法。

     protected override void OnStart(string[] args)
            {
                Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);

            }

            void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
            {
                if (e.Reason == Microsoft.Win32.SessionSwitchReason.SessionUnlock)
                {
                    FileStream fs = new FileStream(@"d:\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);

                    StreamWriter m_streamWriter = new StreamWriter(fs);

                    m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

                    m_streamWriter.WriteLine("mcWindowsService: Service Started" + "Got Unlock Event" + DateTime.Now.ToString() + "\n");

                    m_streamWriter.Flush();

                    m_streamWriter.Close();

                    fs.Close();
                }
            }

    2012年4月18日 2:20

答案

  • 重载ServiceBase.OnSessionChange.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP

    2012年4月18日 3:13
    版主

全部回复