Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Shutdown and Logoff System Events and Windows Services

Shutdown and Logoff System Events and Windows Services

Scheduled Pinned Locked Moved C#
helpcomquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adrianojorge
    wrote on last edited by
    #1

    Good evening 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? Best Regards.

    M S G 3 Replies Last reply
    0
    • A adrianojorge

      Good evening 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? Best Regards.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Can you attach a debugger or at least write some log entries so you can debug what's occurring?

      Mark Salsbery :java:

      A 1 Reply Last reply
      0
      • M Mark Salsbery

        Can you attach a debugger or at least write some log entries so you can debug what's occurring?

        Mark Salsbery :java:

        A Offline
        A Offline
        adrianojorge
        wrote on last edited by
        #3

        Dear Mark, I'm using Visual C# Express 2005 and i can't find the process debugger on my debug tab. But i can present you the code i'm using:

        using System;
        using System.ServiceProcess;
        using System.Threading;
        using System.Windows.Forms;
        using System.Diagnostics;
        using Microsoft.Win32;
        using System.ComponentModel;
        using System.Configuration.Install;
        using System.Net.Mail;

        namespace SimpleServiceCs
        {
        public class SimpleService : ServiceBase
        {
        static void Main(string[] args)
        {
        ServiceBase.Run(new SimpleService());
        }

            protected override void OnStart(string\[\] args)
            {
                EventLog.WriteEntry("SimpleService", "Starting SimpleService");
                new Thread(RunMessagePump).Start();
            }
        
            void RunMessagePump()
            {
                EventLog.WriteEntry("SimpleService.MessagePump", "Starting SimpleService Message Pump");
                Application.Run(new HiddenForm());
            }
        
            protected override void OnStop()
            {
                Application.Exit();
            }
        }
        
        public partial class HiddenForm : Form
        {
            public HiddenForm()
            {
                InitializeComponent();
            }
        
            private void HiddenForm\_Load(object sender, EventArgs e)
            {
                SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents\_SessionEnding);
            }
        
            private void HiddenForm\_FormClosing(object sender, FormClosingEventArgs e)
            {
                SystemEvents.SessionEnding -= new SessionEndingEventHandler(SystemEvents\_SessionEnding);
            }
        

        //****************************************************************************
        // THIS IS THE PART OF CODE STRANGELY NOT WORKING!?!?
        //****************************************************************************

            private void SystemEvents\_SessionEnding(object sender, EventArgs e)
            {
                EventLog.WriteEntry("System Logoff or System Shutdown");
                SendEMail("Your System is being Logged Off or Shutdown!");
            }
        

        //****************************************************************************

            private void SendEmail(string Warning){
        
            //Here goes all the code necessary for sending e-mails from my personal mail account
        
            }
        
        
        }
        
        partial class HiddenForm
        {
            private System.ComponentModel.IContainer components = null;
        
            protected override void Dispose(bool disposing)
        
        M 1 Reply Last reply
        0
        • A adrianojorge

          Dear Mark, I'm using Visual C# Express 2005 and i can't find the process debugger on my debug tab. But i can present you the code i'm using:

          using System;
          using System.ServiceProcess;
          using System.Threading;
          using System.Windows.Forms;
          using System.Diagnostics;
          using Microsoft.Win32;
          using System.ComponentModel;
          using System.Configuration.Install;
          using System.Net.Mail;

          namespace SimpleServiceCs
          {
          public class SimpleService : ServiceBase
          {
          static void Main(string[] args)
          {
          ServiceBase.Run(new SimpleService());
          }

              protected override void OnStart(string\[\] args)
              {
                  EventLog.WriteEntry("SimpleService", "Starting SimpleService");
                  new Thread(RunMessagePump).Start();
              }
          
              void RunMessagePump()
              {
                  EventLog.WriteEntry("SimpleService.MessagePump", "Starting SimpleService Message Pump");
                  Application.Run(new HiddenForm());
              }
          
              protected override void OnStop()
              {
                  Application.Exit();
              }
          }
          
          public partial class HiddenForm : Form
          {
              public HiddenForm()
              {
                  InitializeComponent();
              }
          
              private void HiddenForm\_Load(object sender, EventArgs e)
              {
                  SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents\_SessionEnding);
              }
          
              private void HiddenForm\_FormClosing(object sender, FormClosingEventArgs e)
              {
                  SystemEvents.SessionEnding -= new SessionEndingEventHandler(SystemEvents\_SessionEnding);
              }
          

          //****************************************************************************
          // THIS IS THE PART OF CODE STRANGELY NOT WORKING!?!?
          //****************************************************************************

              private void SystemEvents\_SessionEnding(object sender, EventArgs e)
              {
                  EventLog.WriteEntry("System Logoff or System Shutdown");
                  SendEMail("Your System is being Logged Off or Shutdown!");
              }
          

          //****************************************************************************

              private void SendEmail(string Warning){
          
              //Here goes all the code necessary for sending e-mails from my personal mail account
          
              }
          
          
          }
          
          partial class HiddenForm
          {
              private System.ComponentModel.IContainer components = null;
          
              protected override void Dispose(bool disposing)
          
          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          You should still be able to debug it, but that's a topic for Visual Studio. Do you get the log entry "System Logoff or System Shutdown"? If so, write some log entries in your email code to see if some function is failing.

          Mark Salsbery :java:

          A 1 Reply Last reply
          0
          • A adrianojorge

            Good evening 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? Best Regards.

            S Offline
            S Offline
            SilimSayo
            wrote on last edited by
            #5

            It could be that the machine does not have an SMTP account configure on it. Before your try to get your service to send an e-mail, try sending an e-mail from the it yourself. After you do that, yo must make sure that your windows service is able to use that SMTP e-mail account.

            A 1 Reply Last reply
            0
            • M Mark Salsbery

              You should still be able to debug it, but that's a topic for Visual Studio. Do you get the log entry "System Logoff or System Shutdown"? If so, write some log entries in your email code to see if some function is failing.

              Mark Salsbery :java:

              A Offline
              A Offline
              adrianojorge
              wrote on last edited by
              #6

              Dear Mark, Testing my service with attached debugger and with a breakpoint at my SystemEvent Handler (private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)), when i try to logoff or shutdown my pc, that breakpoint is never reached, because the debugger is first disabled. Best Regards.

              1 Reply Last reply
              0
              • S SilimSayo

                It could be that the machine does not have an SMTP account configure on it. Before your try to get your service to send an e-mail, try sending an e-mail from the it yourself. After you do that, yo must make sure that your windows service is able to use that SMTP e-mail account.

                A Offline
                A Offline
                adrianojorge
                wrote on last edited by
                #7

                Dear Silim :) Thank you for your fast response. That was the first test i've done. The smtp mail account is working fine. Only at SystemEvents Handler (private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)) isn't working. Best regards.

                1 Reply Last reply
                0
                • A adrianojorge

                  Good evening 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? Best Regards.

                  G Offline
                  G Offline
                  ganeshbdas
                  wrote on last edited by
                  #8

                  MailMessage emailmsg = new MailMessage();
                  string ids="aa@gamil.com,bb@gmail.com";
                  string[] mailIDs = .Split(',');

                              emailmsg.From = new MailAddress("ganeshbdas1@gmail.com", "name of email");
                              foreach (string id in mailIDs)
                              {
                                  emailmsg.To.Add(new MailAddress(id, id));
                              }
                              string\[\] stringAM = { "AM" };
                              emailmsg.Subject = "";
                  
                              emailmsg.Body = "";
                              emailmsg.IsBodyHtml = true;
                              emailmsg.Priority = MailPriority.Normal;
                              SmtpClient mailclient = new SmtpClient("smtp.bizmail.yahoo.com");
                  
                              mailclient.Credentials = new System.Net.NetworkCredential("ganeshbdas1@gmail.com", "password");
                              mailclient.Send(emailmsg);
                  
                  A 1 Reply Last reply
                  0
                  • G ganeshbdas

                    MailMessage emailmsg = new MailMessage();
                    string ids="aa@gamil.com,bb@gmail.com";
                    string[] mailIDs = .Split(',');

                                emailmsg.From = new MailAddress("ganeshbdas1@gmail.com", "name of email");
                                foreach (string id in mailIDs)
                                {
                                    emailmsg.To.Add(new MailAddress(id, id));
                                }
                                string\[\] stringAM = { "AM" };
                                emailmsg.Subject = "";
                    
                                emailmsg.Body = "";
                                emailmsg.IsBodyHtml = true;
                                emailmsg.Priority = MailPriority.Normal;
                                SmtpClient mailclient = new SmtpClient("smtp.bizmail.yahoo.com");
                    
                                mailclient.Credentials = new System.Net.NetworkCredential("ganeshbdas1@gmail.com", "password");
                                mailclient.Send(emailmsg);
                    
                    A Offline
                    A Offline
                    adrianojorge
                    wrote on last edited by
                    #9

                    Hi ganeshbdas, Do you want to tell us something?

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups