daily pastebin goal
11%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. How do i know if my windows service is working?
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.ServiceProcess;
  9. using System.Text;
  10. using System.Timers;
  11.  
  12. namespace EmailWindowsService
  13. {
  14.     public partial class MyEmailService : ServiceBase
  15.     {
  16.         private Timer scheduleTimer1 = null;
  17.         private DateTime lastRun;
  18.         private bool flag;
  19.  
  20.         public MyEmailService()
  21.         {
  22.             InitializeComponent();
  23.             if (!System.Diagnostics.EventLog.SourceExists("MySource"))
  24.             {
  25.                 System.Diagnostics.EventLog.CreateEventSource(
  26.                     "MySource", "MyNewLog");
  27.             }
  28.             eventLogEmail.Source = "MySource";
  29.             eventLogEmail.Log = "MyNewLog";
  30.  
  31.             scheduleTimer1 = new Timer();
  32.             scheduleTimer1.Interval = 5 * 60 * 1000;
  33.             scheduleTimer1.Elapsed += new ElapsedEventHandler(scheduleTimer_Elapsed);
  34.         }
  35.  
  36.         protected override void OnStart(string[] args)
  37.         {
  38.             flag = true;
  39.             lastRun = DateTime.Now;
  40.             scheduleTimer.Start();
  41.             eventLogEmail.WriteEntry("Started");
  42.         }
  43.  
  44.         protected override void OnStop()
  45.         {
  46.             scheduleTimer.Stop();
  47.             eventLogEmail.WriteEntry("Stopped");
  48.         }
  49.         protected override void OnPause()
  50.         {
  51.             scheduleTimer.Stop();
  52.             eventLogEmail.WriteEntry("Paused");
  53.         }
  54.         protected override void OnContinue()
  55.         {
  56.             scheduleTimer.Start(); ;
  57.             eventLogEmail.WriteEntry("Continuing");
  58.         }
  59.         protected override void OnShutdown()
  60.         {
  61.             scheduleTimer.Stop();
  62.             eventLogEmail.WriteEntry("ShutDowned");
  63.         }
  64.  
  65.         protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
  66.         {
  67.             RetriveEmailClass Emails = new RetriveEmailClass();
  68.             if (flag == true)
  69.             {
  70.  
  71.                 eventLogEmail.WriteEntry("In getting Email Method");
  72.                 Emails.ServiceEmailMethod();
  73.                 lastRun = DateTime.Now;
  74.                 flag = false;
  75.             }
  76.             else if (flag == false)
  77.             {
  78.                 if (lastRun.Date < DateTime.Now.Date)
  79.                 {
  80.                     Emails.ServiceEmailMethod();
  81.                     eventLogEmail.WriteEntry("In getting Email Method");
  82.                 }
  83.             }
  84.         }
  85.         }
  86.  
  87.  
  88.     }
  89.    
  90. scheduleTimer1.Interval = 1 * 60 * 1000;
  91.    
  92. System.Diagnostics.Debugger.Break();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top