Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

How to set timer in windows service program?

Hi

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

Avatar of bmatumbura
bmatumbura

I don't see a problem with this. It should connect and query the database fine.
This is because SQL server can handle multiple connection from the same application or from multiple applications
Avatar of Gautham Janardhan
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
Avatar of techques

ASKER

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))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.eventLog1 = new System.Diagnostics.EventLog();
            ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
            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);
 
            ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
            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

ASKER CERTIFIED SOLUTION
Avatar of bmatumbura
bmatumbura

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial