Sandeep Vemula

Sandeep Vemula

  • NA
  • 569
  • 144.1k

Windowsservice is not running automatically at perticular time.

Mar 9 2012 11:23 AM
hi all,
i have a service that is installed success.
but it is not running automatically at perticular time.
but whn am click restart the service it is running successfully(sending mails).
here am sending my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Configuration;
using System.Security;
using System.Net;
using System.Windows.Forms;

namespace WindowServiceToSendMails
{
  partial class MailingService : ServiceBase
  {
  private System.Timers.Timer scheduleTimer = null;
  private DateTime lastRun;
  private bool flag;
  MailMessage message = new MailMessage();
  SmtpClient smtpclient = new SmtpClient();
  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["JobConnString"].ConnectionString);
   
  public MailingService()
  {
  InitializeComponent();
  if (!System.Diagnostics.EventLog.SourceExists("EmailSource"))
  {
  System.Diagnostics.EventLog.CreateEventSource("EmailSource", "EmailLog");
  }
  eventLog1.Source = "EmailSource";
  eventLog1.Log = "EmailLog";

  scheduleTimer = new System.Timers.Timer();
  scheduleTimer.Interval = 3 * 60 * 60 * 1000;
  scheduleTimer.Enabled = true;
  scheduleTimer_Elapsed(null, null);
  //scheduleTimer.Elapsed+=new ElapsedEventHandler(scheduleTimer_Elapsed);
  //if am used this the method is not calling.so am used above method to calling to this method.
  }

  protected override void OnStart(string[] args)
  {
  flag = true;
  lastRun = DateTime.Now;
  scheduleTimer.Start();
  eventLog1.WriteEntry("Started");
  ServiceEmailMethod();
  }


  protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
  {
  if (flag == true)
  {
  ServiceEmailMethod();
  lastRun = DateTime.Now;
  flag = false;
  }
  else if (flag == false)
  {
  if (lastRun.Date < DateTime.Now.Date)
  {
  ServiceEmailMethod();
  }
  }
  }

  private void ServiceEmailMethod()
  {
  string DateTime = System.DateTime.Now.DayOfWeek.ToString();
  eventLog1.WriteEntry("day is checking");
  if (DateTime.ToLower() == "MonDay".ToLower())
  {
  if (Convert.ToInt32(System.DateTime.Now.ToString("HH")) == 9)
  {
  LogMessage("Service Started");
  }
  }
  else if (DateTime.ToLower() == "TuesDay".ToLower())
  {
  if (Convert.ToInt32(System.DateTime.Now.ToString("HH")) == 12)
  {
  LogMessage("Service Started");
  }
  }
  else if (DateTime.ToLower() == "WednesDay".ToLower())
  {
  if (Convert.ToInt32(System.DateTime.Now.ToString("HH")) == 15)
  {
  LogMessage("Service Started");
  }
  }
  if (DateTime.ToLower() == "ThursDay".ToLower())
  {
  if (Convert.ToInt32(System.DateTime.Now.ToString("HH")) == 13)
  {
  eventLog1.WriteEntry("Method calling");
  LogMessage("Service Started");
  }
  }
  if (DateTime.ToLower() == "FriDay".ToLower())
  {
  if (Convert.ToInt32(System.DateTime.Now.ToString("HH")) == 21)
  {
  LogMessage("Service Started");
  }
  }
  if (DateTime.ToLower() == "SaturDay".ToLower())
  {
  if (Convert.ToInt32(System.DateTime.Now.ToString("HH")) == 0)
  {
  LogMessage("Service Started");
  }
  }
  if (DateTime == "SunDay")
  {
  if (Convert.ToInt32(System.DateTime.Now.ToString("HH")) == 3)
  {
  LogMessage("Service Started");
  }
  }

  }

  private void LogMessage(string p)
  {
  try
  {
  eventLog1.WriteEntry("In Sending Email Method");
  DataSet ds = new DataSet();
  SqlCommand cmd1 = new SqlCommand("EC_IUD_EmailMaster", con);
  cmd1.CommandType = CommandType.StoredProcedure;
  cmd1.Parameters.AddWithValue("@iMode", 106);
  cmd1.Parameters.AddWithValue("@Status", SqlDbType.Int).Direction = ParameterDirection.Output;
  SqlDataAdapter adap1 = new SqlDataAdapter(cmd1);
  adap1.Fill(ds, "MessageBody");

  SqlCommand cmd = new SqlCommand("EC_IUD_EmailMaster", con);
  cmd.CommandType = CommandType.StoredProcedure;
  cmd.Parameters.AddWithValue("@iMode", 105);
  cmd.Parameters.AddWithValue("@iCntrws", 999);
  cmd.Parameters.AddWithValue("@Status", SqlDbType.Int).Direction = ParameterDirection.Output;
  SqlDataAdapter adap = new SqlDataAdapter(cmd);
  adap.Fill(ds, "EmailAddress");
  string currentdatetime = DateTime.Now.Year + "." + DateTime.Now.Month + "." + DateTime.Now.Day + " " + DateTime.Now.Hour + (":") + DateTime.Now.Minute + (":") + DateTime.Now.Second;

  foreach (DataRow dr in ds.Tables[1].Rows)
  {
  try
  {
  smtpclient.Host = "sifinit0002";
  MailMessage message = new MailMessage("[email protected]", dr["Email"].ToString());
  message.Body = ds.Tables[0].Rows[0]["MessageBody"].ToString();
  message.Subject = "Hi  ,"+dr["FName"].ToString()+dr["LName"].ToString();
  smtpclient.Credentials = new System.Net.NetworkCredential("", "");
  smtpclient.Port = 25;
  smtpclient.Send(message);
  eventLog1.WriteEntry("mail sent");
  SqlCommand cmd2 = new SqlCommand("EC_IUD_EmailMaster", con);
  cmd2.CommandType = CommandType.StoredProcedure;
  cmd2.Parameters.AddWithValue("@iMode", 104);
  cmd2.Parameters.AddWithValue("@EMail", dr["Email"].ToString());
  cmd2.Parameters.AddWithValue("@Status", SqlDbType.Int).Direction = ParameterDirection.Output;
  con.Open();
  cmd2.ExecuteNonQuery();
  con.Close();
  }
  catch (Exception ex)
  {
  eventLog1.WriteEntry("Message Sent FAILED to - " + dr["Email"].ToString());
  }
  }
   
  }
  catch (FormatException ex)
  {
  throw ex;
  }
   
  finally
  {
  con.Close();
  }
  }
   
  protected override void OnShutdown()
  {
  scheduleTimer.Stop();
  eventLog1.WriteEntry("Stopped");
  }

  protected override void OnContinue()
  {
  scheduleTimer.Start();
  eventLog1.WriteEntry("Paused");
  }

  protected override void OnStop()
  {
  scheduleTimer.Stop();
  eventLog1.WriteEntry("Continuing");
  }

  protected override void OnPause()
  {
  scheduleTimer.Stop();
  eventLog1.WriteEntry("ShutDowned");
  }


  }
}

Answers (3)