How to set timer in windows service program?

Posted on 2009-04-08
Last Modified: 2013-12-17

I built a windows service program which needs to keep on checking data in mssql db for every minute.

In Modules.Systems.UpdateProcess, there have another 2 functions need to connect to different tables in the same DB.

If I set timer2, timer3 and interval = 360000, will they be crashed? as all 3 objects connect to DB in the same time.  

Modules.Systems.UpdateProcess UP = new Modules.Systems.UpdateProcess();

            this.timer1 = new System.Timers.Timer();

            this.timer1.Interval = 360000; 

            this.timer1.Elapsed += new ElapsedEventHandler(UP.UpdateBalance);

Open in new window

Question by:techques
  • 3
LVL 11

Expert Comment

ID: 24096182
I don't see a problem with this. It should connect and query the database fine.
LVL 11

Expert Comment

ID: 24096191
This is because SQL server can handle multiple connection from the same application or from multiple applications
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 24096517
do they use the same connection or different connections ? if they are connectiong to the db using a singleton object then the code will fail

Author Comment

ID: 24098610
Yes, I set different connection, but the problem is the code in 2nd and 3rd timers did not execute.
private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)


            if (disposing && (components != null))






        private void InitializeComponent()


            components = new System.ComponentModel.Container();

            this.eventLog1 = new System.Diagnostics.EventLog();


            Modules.Systems.Process UP1 = new Modules.Systems.Process();

            Modules.Systems.Process UP2 = new Modules.Systems.Process();

            Modules.Systems.Process UP3 = new Modules.Systems.Process();

            this.eventLog1.Log = "Log";

            this.eventLog1.Source = "LogSource";

            this.timer1 = new System.Timers.Timer();

            this.timer1.Interval = 360000; 

            this.timer1.Elapsed += new ElapsedEventHandler(UP1.UpdateClientABalance);

            this.timer2 = new System.Timers.Timer();

            this.timer2.Interval = 360000; 

            this.timer2.Elapsed += new ElapsedEventHandler(UP2.UpdateClientBBalance);

            this.timer3 = new System.Timers.Timer();

            this.timer3.Interval = 360000;

            this.timer3.Elapsed += new ElapsedEventHandler(UP3.UpdateClientCBalance);


            this.ServiceName = "Processing";


        private System.Timers.Timer timer1;

        private System.Timers.Timer timer2;

        private System.Timers.Timer timer3;

        private System.Diagnostics.EventLog eventLog1;

Open in new window

LVL 11

Accepted Solution

bmatumbura earned 50 total points
ID: 24098938
Insert the following statements near the end of the InitializeComponent() routine (but before the (System.ComponentModel.ISupportInitialize ...) statement):



Open in new window


Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now