how to identify logon event in window service

后端 未结 3 446
醉酒成梦
醉酒成梦 2021-01-16 14:50

i have a windows service that get user details and save the result into log text file. and, my problem is when i log off my system and login again ie without restarting the

相关标签:
3条回答
  • 2021-01-16 15:18

    Take a look into the SystemEvents class, here's the MSDN link.

    Relevant in your case are the exposed events SessionEnded, SessionEnding and SessionSwitch and potentially PowerModeChanged.

    A quick example might look like this:

    SystemEvents.SessionSwitch += OnSessionSwitch;
    
    void OnSessionSwitch(object sender, SessionSwitchEventArgs e)
    {
        //implement your logic here
    }
    
    0 讨论(0)
  • 2021-01-16 15:21

    These events are not raised unless a message loop is provided; manually by adding a hidden form (or may be allowing the Service to interact with the desktop - not sure never tired, may be not recommended).

    I'd a similar issue with one of the services not receiving TimeZone changes, so had to add a hidden form to the service.

    Here is one of the examples as to how to solve the issue.

    And below is what I did to solve my issue:

    Added the TimeZoneForm to the service.sln;

    And in the Service's OnStart add this code : new System.Threading.Thread(RunMessagePump).Start();

    And add this method to the service file:

    private void RunMessagePump()
            {            
                Application.Run(new TimeZoneForm.TimeZoneForm());
            }
    
    
    internal class TimeZoneForm : Form
        {
            public TimeZoneForm()
            {
                InitializeComponent();
            }
    
            private void TimeZoneForm_Load(object sender, EventArgs e)
            {
                SystemEvents.TimeChanged += SystemEvents_TimeChanged;            
            }
    
            private void TimeZoneForm_Closing(object sender, FormClosingEventArgs e)
            {
                SystemEvents.TimeChanged -= SystemEvents_TimeChanged;            
            }
    
            private void SystemEvents_TimeChanged(object sender, EventArgs e)
            {
                System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();
            }
    
            private System.ComponentModel.IContainer components = null;
    
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            private void InitializeComponent()
            {
                this.SuspendLayout();
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(0, 0);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.Name = "TimeZoneForm";
                this.Text = "TimeZoneForm";
                this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
                this.Load += this.TimeZoneForm_Load;
                this.FormClosing += this.TimeZoneForm_Closing;
                this.ResumeLayout(false);
    
            }
        }
    
    0 讨论(0)
  • 2021-01-16 15:30

    http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onsessionchange.aspx

    This may be your best bet, as Vista and Win7 handle the user sessions much like a terminal server would. This should let you handle session changes and it gives a structure with the relevant information, if you want session ID or reason for session change (logon / logoff / lock etc)

    0 讨论(0)
提交回复
热议问题