Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 434737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:18:17+00:00 2026-05-12T20:18:17+00:00

I’ve written a Windows Service in C# that basically checks my db every minute

  • 0

I’ve written a Windows Service in C# that basically checks my db every minute for orders, generates a PDF from these orders, and emails it.

The logic works perfectly in my tests etc..

When i create the service, and install it using the setup project, when I go to start the service in the services mmc, I get:

error 1053 the service did not respond to the start or control request in a timely fashion

My OnStart method looks like this:

protected override void OnStart(string[] args)
{
    //writeToWindowsEventLog("Service started", EventLogEntryType.Information);
    timer.Enabled = true;
}

Basically, just enables the timer… so theres no process intensive call there.

Where am I going wrong?

I’ve tried setting the startup account to local system, network service etc… nothing works!

Edit:

Here is my code: (processPurchaseOrders is the method where the db is queried and pdf’s are generated etc…)

public partial class PurchaseOrderDispatcher : ServiceBase
{
    //this is the main timer of the service
    private System.Timers.Timer timer;

    public PurchaseOrderDispatcher()
    {
        InitializeComponent();
    }

    // The main entry point for the process
    static void Main()
    {
      #if (!DEBUG)
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new PurchaseOrderDispatcher() };
        ServiceBase.Run(ServicesToRun);
      #else //debug code
        PurchaseOrderDispatcher service = new PurchaseOrderDispatcher();
        service.processPurchaseOrders();
      #endif
    }

    private void InitializeComponent()
    {
        this.CanPauseAndContinue = true;
        this.ServiceName = "Crocus_PurchaseOrderGenerator";
    }

    private void InitTimer()
    {
        timer = new System.Timers.Timer();

        //wire up the timer event
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

        //set timer interval
        var timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalInSeconds"]);
        timer.Interval = (timeInSeconds * 1000); // timer.Interval is in milliseconds, so times above by 1000

        timer.Enabled = true;
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
            components.Dispose();

        base.Dispose(disposing);
    }

    protected override void OnStart(string[] args)
    {
        //instantiate timer
        Thread t = new Thread(new ThreadStart(this.InitTimer));
        t.Start();
    }

    protected override void OnStop()
    {
        //turn off the timer.
        timer.Enabled = false;
    }

    protected override void OnPause()
    {
        timer.Enabled = false;

        base.OnPause();
    }

    protected override void OnContinue()
    {
        timer.Enabled = true;

        base.OnContinue();
    }

    protected void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        processPurchaseOrders();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:18:17+00:00Added an answer on May 12, 2026 at 8:18 pm

    From MSDN:

    Do not use the constructor to perform processing that should be in
    OnStart. Use OnStart to handle all initialization of your service. The
    constructor is called when the application’s executable runs, not when
    the service runs. The executable runs before OnStart. When you
    continue, for example, the constructor is not called again because the
    SCM already holds the object in memory. If OnStop releases resources
    allocated in the constructor rather than in OnStart, the needed
    resources would not be created again the second time the service is
    called.

    If your timer is not initialized in the OnStart call, this could be a problem.
    I would also check the type of timer, make sure its a System.Timers.Timer for Services. Here is an example of how to setup the timer in a Windows service.

    //TODONT: Use a Windows Service just to run a scheduled process

    I tried your code, and it seems ok. The only difference I had was to hard code the timer value (Service1.cs). Let me know if the below doesn’t work.

    Service1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    using System.Timers;
    using System.Threading;
    
    namespace WindowsServiceTest
    {
        public partial class Service1 : ServiceBase
        {
            private System.Timers.Timer timer;
    
            public Service1()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                //instantiate timer
                Thread t = new Thread(new ThreadStart(this.InitTimer)); 
                t.Start();
            }
    
            protected override void OnStop()
            {
                timer.Enabled = false;
            }
    
             private void InitTimer()  
             {     
                 timer = new System.Timers.Timer();  
                 //wire up the timer event 
                 timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
                 //set timer interval   
                 //var timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalInSeconds"]); 
                 double timeInSeconds = 3.0;
                 timer.Interval = (timeInSeconds * 1000); 
                 // timer.Interval is in milliseconds, so times above by 1000 
                 timer.Enabled = true;  
             }
    
            protected void timer_Elapsed(object sender, ElapsedEventArgs e) 
            {
                int timer_fired = 0;
            }
        }
    }
    

    Service1.Designer.cs

    namespace WindowsServiceTest
    {
        partial class Service1
        {
            /// <summary> 
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Component Designer generated code
    
            /// <summary> 
            /// Required method for Designer support - do not modify 
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                components = new System.ComponentModel.Container();
                this.ServiceName = "Service1";
                this.CanPauseAndContinue = true;
            }
    
            #endregion
        }
    }
    

    I just created a blank Windows Service project and add the below so I could run installutil.exe and attach to the above to see if the event was firing (and it did).

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ComponentModel;
    using System.ServiceProcess;
    
    namespace WindowsServiceTest
    {
        [RunInstaller(true)]
        public class MyServiceInstaller : System.Configuration.Install.Installer
        {
            public MyServiceInstaller()
            {
                ServiceProcessInstaller process = new ServiceProcessInstaller();
    
                process.Account = ServiceAccount.LocalSystem;
    
                ServiceInstaller serviceAdmin = new ServiceInstaller();
    
                serviceAdmin.StartType = ServiceStartMode.Manual;
                serviceAdmin.ServiceName = "Service1";
                serviceAdmin.DisplayName = "Service1 Display Name";
                Installers.Add(process);
                Installers.Add(serviceAdmin);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 247k
  • Answers 247k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes. Once there are no references to HierarchicalData, it will… May 13, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer Your function definition is incorrect. It should take a list… May 13, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer Have you tried this? //myparent/mychild[text() = 'foo'] Alternatively, you can… May 13, 2026 at 8:37 am

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.