473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Shutdown and Logoff System Events and Windows Services

Good Morning to all :)

Currently i'm developing a Windows Service in order to automatically send me e-mails with my remote computer operating status.
I already checked MSDN documentation about this subject http://msdn.microsoft.com/en-us/library/ycy63t34.aspx and changed the code to use on my situation. But... i don't receive e-mails from it when the remote computer is normally turned off.

Someone could help me with this issue?

The code i'm using is:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.ServiceProcess;
  3. using System.Threading;
  4. using System.Windows.Forms;
  5. using System.Diagnostics;
  6. using Microsoft.Win32;
  7. using System.ComponentModel;
  8. using System.Configuration.Install;
  9. using System.Net.Mail;
  10.  
  11. namespace SimpleServiceCs
  12. {
  13.     public class SimpleService : ServiceBase
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             ServiceBase.Run(new SimpleService());
  18.         }
  19.  
  20.         protected override void OnStart(string[] args)
  21.         {
  22.             EventLog.WriteEntry("SimpleService", "Starting SimpleService");
  23.             new Thread(RunMessagePump).Start();
  24.         }
  25.  
  26.         void RunMessagePump()
  27.         {
  28.             EventLog.WriteEntry("SimpleService.MessagePump", "Starting SimpleService Message Pump");
  29.             Application.Run(new HiddenForm());
  30.         }
  31.  
  32.         protected override void OnStop()
  33.         {
  34.             Application.Exit();
  35.         }
  36.     }
  37.  
  38.     public partial class HiddenForm : Form
  39.     {
  40.         public HiddenForm()
  41.         {
  42.             InitializeComponent();
  43.         }
  44.  
  45.         private void HiddenForm_Load(object sender, EventArgs e)
  46.         {
  47.             SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);
  48.         }
  49.  
  50.         private void HiddenForm_FormClosing(object sender, FormClosingEventArgs e)
  51.         {
  52.             SystemEvents.SessionEnding -= new SessionEndingEventHandler(SystemEvents_SessionEnding);
  53.         }
  54. //****************************************************************************
  55. //          THIS IS THE PART OF CODE STRANGELY NOT WORKING!?!?
  56. //****************************************************************************
  57.  
  58.         private void SystemEvents_SessionEnding(object sender, EventArgs e)
  59.         {
  60.             EventLog.WriteEntry("System Logoff or System Shutdown");
  61.             SendEMail("Your System is being Logged Off or Shutdown!");
  62.         }
  63.  
  64. //****************************************************************************
  65.  
  66.         private void SendEmail(string Warning){
  67.  
  68.         //Here goes all the code necessary for sending e-mails from my personal mail account
  69.  
  70.         }
  71.  
  72.  
  73.     }
  74.  
  75.     partial class HiddenForm
  76.     {
  77.         private System.ComponentModel.IContainer components = null;
  78.  
  79.         protected override void Dispose(bool disposing)
  80.         {
  81.             if (disposing && (components != null))
  82.             {
  83.                 components.Dispose();
  84.             }
  85.             base.Dispose(disposing);
  86.         }
  87.  
  88.         private void InitializeComponent()
  89.         {
  90.             this.SuspendLayout();
  91.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  92.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  93.             this.ClientSize = new System.Drawing.Size(0, 0);
  94.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  95.             this.Name = "HiddenForm";
  96.             this.Text = "HiddenForm";
  97.             this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
  98.             this.Load += new System.EventHandler(this.HiddenForm_Load);
  99.             this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.HiddenForm_FormClosing);
  100.             this.ResumeLayout(false);
  101.  
  102.         }
  103.     }
  104.  
  105.     [RunInstaller(true)]
  106.     public class SimpleInstaller : Installer
  107.     {
  108.         private ServiceInstaller serviceInstaller;
  109.         private ServiceProcessInstaller processInstaller;
  110.  
  111.         public SimpleInstaller()
  112.         {
  113.             processInstaller = new ServiceProcessInstaller();
  114.             serviceInstaller = new ServiceInstaller();
  115.  
  116.             // Service will run under system account
  117.             processInstaller.Account = ServiceAccount.LocalSystem;
  118.  
  119.             // Service will have Start Type of Manual
  120.             serviceInstaller.StartType = ServiceStartMode.Automatic;
  121.  
  122.             serviceInstaller.ServiceName = "Simple Service";
  123.  
  124.             Installers.Add(serviceInstaller);
  125.             Installers.Add(processInstaller);
  126.         }
  127.     }
  128. }
Best Regards.
Nov 19 '11 #1
3 4841
arie
64
First, when your system shuts down, it probably kills your application threads even if they didn't finish their work.

Second, using any form of interaction with GUI inside Windows service is bad bad bad! I don't know if the fact that your Form is hidden helps, or is it just "hiding" eventual problems, too. Maybe that's the problem? Why don't you just override methods that are already there, just as you override OnStart, and forget about forms?

In ServiceBase class there are methods:
Expand|Select|Wrap|Line Numbers
  1. protected override void OnShutdown()
  2. protected override void OnSessionChange(SessionChangeDescription changeDescription)
and more.
Nov 22 '11 #2
Hi Arie,

Thanks for your response.

Yes, probably it is the shutdown sequence that is not allowing my service to work correctly.

Regarding OnShutdown() method, i already tested it, but the same result... not working :(

I'm getting out of solutions for this problem. Is it possible to change the windows services shutdown sequence?

Best regards.
Nov 22 '11 #3
arie
64
Maybe, only on shutdown, try sending the mail without using other threads? I wonder if the "system shutdown sequence" will wait for the main thread of the service to complete before the service terminates?
Nov 22 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bill Sonia | last post by:
I'm written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that once my send mail code is...
8
by: Bill Sonia | last post by:
I've written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that for OnShutDown, once my...
7
by: Mullin Yu | last post by:
if i put the same code at the windows application or console, i can logon to the computer. but, if i put the same code at the windows service and start it, i still can't logon to the machine. ...
1
by: Jack David | last post by:
I need direction on how to create a program that will start multiple windows services. Each service will monitor a specific directory using FileSystemWatcher. I have the file system watcher part...
8
by: Giox | last post by:
Hello everybody, I have problem using Windows Services. I want to host a server application on in a windows service and I'm not able to do that. In effect when I try to start the service I receive...
3
by: Yves Royer | last post by:
Hi all, I have a little question about Windows Services. For my app i need 3 different Windows Services, each with its own functionality. I'm trying to make a test service to see what happens...
2
by: Chris Podmore | last post by:
I don't know if this is the correct newsgroup but I couldn't find one for Windows Services Is it possible to check that a Windows Service is still running from another machine The idea being a...
2
by: Val3 | last post by:
Hi all. I need to build dll(s) and windows services using VB .NET 2005 Express. When I make File/New project the windows contain only Windows application, Windows control library, Console...
15
by: =?Utf-8?B?RWxpb3Ro?= | last post by:
I try to install Windows Services but it show this error during the installation process, "Insufficient System resources exist to complete the requested service." I created this Services in VB...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.