Shutdown and Logoff System Events and Windows Services

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Good Morning to all <img title="Smilie" src="http://images.daniweb.com/smilies/sleek/qqb014.gif" border="0" alt=" <br/>
<br/>
Currently im developing a Windows Service in order to automatically send me e-mails with my remote computer operating status.<br/>
I already checked MSDN documentation about this subject http://msdn.microsoft.com/en-us/library/ycy63t34.aspx" target="_blank
http://msdn.microsoft.com/en-us/library/ycy63t34.aspx and changed the code to use on my situation. But... i dont receive e-mails from it when the remote computer is normally turned off.<br/>
<br/>
Someone could help me with this issue?<br/>
<br/>
The code im using is:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.ServiceProcess;
<span style="color:Blue; using System.Threading;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Diagnostics;
<span style="color:Blue; using Microsoft.Win32;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Configuration.Install;
<span style="color:Blue; using System.Net.Mail;

<span style="color:Blue; namespace SimpleServiceCs
{
<span style="color:Blue; public <span style="color:Blue; class SimpleService : ServiceBase
{
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
ServiceBase.Run(<span style="color:Blue; new SimpleService());
}

<span style="color:Blue; protected <span style="color:Blue; override <span style="color:Blue; void OnStart(<span style="color:Blue; string[] args)
{
EventLog.WriteEntry(<span style="color:#A31515; "SimpleService", <span style="color:#A31515; "Starting SimpleService");
<span style="color:Blue; new Thread(RunMessagePump).Start();
}

<span style="color:Blue; void RunMessagePump()
{
EventLog.WriteEntry(<span style="color:#A31515; "SimpleService.MessagePump", <span style="color:#A31515; "Starting SimpleService Message Pump");
Application.Run(<span style="color:Blue; new HiddenForm());
}

<span style="color:Blue; protected <span style="color:Blue; override <span style="color:Blue; void OnStop()
{
Application.Exit();
}
}

<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class HiddenForm : Form
{
<span style="color:Blue; public HiddenForm()
{
InitializeComponent();
}

<span style="color:Blue; private <span style="color:Blue; void HiddenForm_Load(<span style="color:Blue; object sender, EventArgs e)
{
SystemEvents.SessionEnding += <span style="color:Blue; new SessionEndingEventHandler(SystemEvents_SessionEnding);
}

<span style="color:Blue; private <span style="color:Blue; void HiddenForm_FormClosing(<span style="color:Blue; object sender, FormClosingEventArgs e)
{
SystemEvents.SessionEnding -= <span style="color:Blue; new SessionEndingEventHandler(SystemEvents_SessionEnding);
}
<span style="color:Green; //****************************************************************************
<span style="color:Green; // THIS IS THE PART OF CODE STRANGELY NOT WORKING!?!?
<span style="color:Green; //****************************************************************************

<span style="color:Blue; private <span style="color:Blue; void SystemEvents_SessionEnding(<span style="color:Blue; object sender, EventArgs e)
{
EventLog.WriteEntry(<span style="color:#A31515; "System Logoff or System Shutdown");
SendEMail(<span style="color:#A31515; "Your System is being Logged Off or Shutdown!");
}

<span style="color:Green; //****************************************************************************

<span style="color:Blue; private <span style="color:Blue; void SendEmail(<span style="color:Blue; string Warning){

<span style="color:Green; //Here goes all the code necessary for sending e-mails from my personal mail account

}


}

<span style="color:Blue; partial <span style="color:Blue; class HiddenForm
{
<span style="color:Blue; private System.ComponentModel.IContainer components = <span style="color:Blue; null;

<span style="color:Blue; protected <span style="color:Blue; override <span style="color:Blue; void Dispose(<span style="color:Blue; bool disposing)
{
<span style="color:Blue; if (disposing && (components != <span style="color:Blue; null))
{
components.Dispose();
}
<span style="color:Blue; base.Dispose(disposing);
}

<span style="color:Blue; private <span style="color:Blue; void InitializeComponent()
{
<span style="color:Blue; this.SuspendLayout();
<span style="color:Blue; this.AutoScaleDimensions = <span style="color:Blue; new System.Drawing.SizeF(6F, 13F);
<span style="color:Blue; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
<span style="color:Blue; this.ClientSize = <span style="color:Blue; new System.Drawing.Size(0, 0);
<span style="color:Blue; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
<span style="color:Blue; this.Name = <span style="color:#A31515; "HiddenForm";
<span style="color:Blue; this.Text = <span style="color:#A31515; "HiddenForm";
<span style="color:Blue; this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
<span style="color:Blue; this.Load += <span style="color:Blue; new System.EventHandler(<span style="color:Blue; this.HiddenForm_Load);
<span style="color:Blue; this.FormClosing += <span style="color:Blue; new System.Windows.Forms.FormClosingEventHandler(<span style="color:Blue; this.HiddenForm_FormClosing);
<span style="color:Blue; this.ResumeLayout(<span style="color:Blue; false);

}
}

[RunInstaller(<span style="color:Blue; true)]
<span style="color:Blue; public <span style="color:Blue; class SimpleInstaller : Installer
{
<span style="color:Blue; private ServiceInstaller serviceInstaller;
<span style="color:Blue; private ServiceProcessInstaller processInstaller;

<span style="color:Blue; public SimpleInstaller()
{
processInstaller = <span style="color:Blue; new ServiceProcessInstaller();
serviceInstaller = <span style="color:Blue; new ServiceInstaller();

<span style="color:Green; // Service will run under system account
processInstaller.Account = ServiceAccount.LocalSystem;

<span style="color:Green; // Service will have Start Type of Manual
serviceInstaller.StartType = ServiceStartMode.Automatic;

serviceInstaller.ServiceName = <span style="color:#A31515; "Simple Service";

Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}
[/code]
Best Regards.<br/>

View the full article
 
Back
Top