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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Service Starting Problem!

Service Starting Problem!

Scheduled Pinned Locked Moved C#
helpsysadminwindows-adminperformancetutorial
7 Posts 3 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.
  • B Offline
    B Offline
    backSlashZero
    wrote on last edited by
    #1

    I have created a windows service to send email on daily basis. It has a timer object that fires an event after 24 hours,

    private System.Timers.Timer timer = new System.Timers.Timer (86400000);

    In timer event I do my working i.e. I send my emails, something like this:

    private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
    {
    eventLog1.WriteEntry("Calling web service for Sending Reminders on " + DateTime.Now.ToLongDateString());
    if (EmailObj.SendReminder()) // if sending email is sussful
    {
    eventLog1.WriteEntry("The operation was successfully completed on " + DateTime.Now.ToLongDateString());;
    }
    else
    {
    eventLog1.WriteEntry("There was some error, the operation failed on " + DateTime.Now.ToLongDateString());;
    }

    if (DateTime.Today.Day == 1)
    {
    	// first date of month; send emails	
    }
    

    }

    The problem is, I have installed the service on Windows server 2003, but the service starts and stops immediately with the message “The MyServiceName service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service” I can see the event log entries that I did on OnStart event of the service; also the service is working fine on Windows XP. Please help me I have to run this service on MS Windows server 2003 forever to send emails on daily basis,

    cout << "\0"; // its backSlashZero

    N realJSOPR 2 Replies Last reply
    0
    • B backSlashZero

      I have created a windows service to send email on daily basis. It has a timer object that fires an event after 24 hours,

      private System.Timers.Timer timer = new System.Timers.Timer (86400000);

      In timer event I do my working i.e. I send my emails, something like this:

      private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
      {
      eventLog1.WriteEntry("Calling web service for Sending Reminders on " + DateTime.Now.ToLongDateString());
      if (EmailObj.SendReminder()) // if sending email is sussful
      {
      eventLog1.WriteEntry("The operation was successfully completed on " + DateTime.Now.ToLongDateString());;
      }
      else
      {
      eventLog1.WriteEntry("There was some error, the operation failed on " + DateTime.Now.ToLongDateString());;
      }

      if (DateTime.Today.Day == 1)
      {
      	// first date of month; send emails	
      }
      

      }

      The problem is, I have installed the service on Windows server 2003, but the service starts and stops immediately with the message “The MyServiceName service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service” I can see the event log entries that I did on OnStart event of the service; also the service is working fine on Windows XP. Please help me I have to run this service on MS Windows server 2003 forever to send emails on daily basis,

      cout << "\0"; // its backSlashZero

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      What is there in your OnStart() method ? Is your timer enabled ?

      backSlashZero wrote:

      private System.Timers.Timer timer = new System.Timers.Timer (86400000);

      You are keeping the thread sleeping for such a long time. Better method to do this is to write a console application which sends mail and exits automatically. Use windows scheduler to schedule this exe to run at required time. So you are not keeping any threads sleeping for long time. Windows service should be used, if it has to do processing all time when it runs, not for scheduling like this, AFAIK.

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      B 1 Reply Last reply
      0
      • B backSlashZero

        I have created a windows service to send email on daily basis. It has a timer object that fires an event after 24 hours,

        private System.Timers.Timer timer = new System.Timers.Timer (86400000);

        In timer event I do my working i.e. I send my emails, something like this:

        private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
        eventLog1.WriteEntry("Calling web service for Sending Reminders on " + DateTime.Now.ToLongDateString());
        if (EmailObj.SendReminder()) // if sending email is sussful
        {
        eventLog1.WriteEntry("The operation was successfully completed on " + DateTime.Now.ToLongDateString());;
        }
        else
        {
        eventLog1.WriteEntry("There was some error, the operation failed on " + DateTime.Now.ToLongDateString());;
        }

        if (DateTime.Today.Day == 1)
        {
        	// first date of month; send emails	
        }
        

        }

        The problem is, I have installed the service on Windows server 2003, but the service starts and stops immediately with the message “The MyServiceName service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service” I can see the event log entries that I did on OnStart event of the service; also the service is working fine on Windows XP. Please help me I have to run this service on MS Windows server 2003 forever to send emails on daily basis,

        cout << "\0"; // its backSlashZero

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3

        Add a keep-alive thread that doesn't do anything but sleeps for 4.5 seconds.

        protected void KeepAliveThread()
        {
        ServiceController controller = new ServiceController(this.ServiceName);
        while (true)
        {
        if (controller.Status == ServiceControllerStatus.Running)
        {
        Thread.Sleep(4500);
        }
        }
        }

        In your OnStart() method, do this:

        Thread thread = new Thread(new ThreadStart(KeepAliveThread));
        thread.Start();

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        B 1 Reply Last reply
        0
        • realJSOPR realJSOP

          Add a keep-alive thread that doesn't do anything but sleeps for 4.5 seconds.

          protected void KeepAliveThread()
          {
          ServiceController controller = new ServiceController(this.ServiceName);
          while (true)
          {
          if (controller.Status == ServiceControllerStatus.Running)
          {
          Thread.Sleep(4500);
          }
          }
          }

          In your OnStart() method, do this:

          Thread thread = new Thread(new ThreadStart(KeepAliveThread));
          thread.Start();

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          B Offline
          B Offline
          backSlashZero
          wrote on last edited by
          #4

          Thanks! I tried it out but problem is still there :confused:

          cout << "\0"; // its backSlashZero

          realJSOPR 1 Reply Last reply
          0
          • N N a v a n e e t h

            What is there in your OnStart() method ? Is your timer enabled ?

            backSlashZero wrote:

            private System.Timers.Timer timer = new System.Timers.Timer (86400000);

            You are keeping the thread sleeping for such a long time. Better method to do this is to write a console application which sends mail and exits automatically. Use windows scheduler to schedule this exe to run at required time. So you are not keeping any threads sleeping for long time. Windows service should be used, if it has to do processing all time when it runs, not for scheduling like this, AFAIK.

            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

            B Offline
            B Offline
            backSlashZero
            wrote on last edited by
            #5

            N a v a n e e t h wrote:

            Better method to do this is to write a console application which sends mail and exits automatically. Use windows scheduler to schedule this exe to run at required time.

            Thanks I am going to do this now :~

            cout << "\0"; // its backSlashZero

            1 Reply Last reply
            0
            • B backSlashZero

              Thanks! I tried it out but problem is still there :confused:

              cout << "\0"; // its backSlashZero

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              Are you doing the following in OnStart()?

              timer.Enabled = true;
              timer.Start();
              

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              B 1 Reply Last reply
              0
              • realJSOPR realJSOP

                Are you doing the following in OnStart()?

                timer.Enabled = true;
                timer.Start();
                

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                B Offline
                B Offline
                backSlashZero
                wrote on last edited by
                #7

                Yes i did!

                protected override void OnStart(string[] args)
                {
                eventLog1.WriteEntry("In OnStart: Calling web service for Sending Reminders on "
                + DateTime.Now.ToLongDateString());

                EmailObj.SendReminder(); \\\\send reminders if any!
                timer.Enabled = true;
                timer.Start();
                

                }

                cout << "\0"; // its backSlashZero

                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