Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want my windows service to keep checking SQL server table every 30 secs with one timer and get another interval timer value from this table to do another job.

How can I achieve this?


What I have tried:

C#
protected override void OnStart(string[] args)
        {
            try
            {
                timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
                timer1.Interval = 30000;
                timer1.Enabled = true;
                timer1.Start();

            }
            catch (Exception ex)
            {
                ErrorLogging(ex);
            }
            
        }

        protected override void OnStop()
        {
            timer1.Enabled = false;
        }


        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {

                con = new SqlConnection("server=servername; database=databasename;uid=username; password=password");
                con.Open();

                cmd = new SqlCommand("Select TOP(1) [IntervalTime] from [dbo].[Intervals] ORDER BY [IntervalTime] DESC", con);
                int interval = cmd.ExecuteNonQuery();

                timer2.Elapsed += new ElapsedEventHandler(timer2_Elapsed);
                timer2.Interval = interval;
                timer2.Enabled = true;
                timer2.Start();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            finally
            {
                cmd.Dispose();
                con.Dispose();
            }

        }


        private void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
// Do other job
         }

Can anyone please change this code to make it working.
Posted
Updated 10-May-18 19:28pm
Comments
CS2011 11-May-18 1:23am    
You should avoid timers. This will give you a performance hit. if your requirement is to check if some data has been updated in table and perform some work based on that i would recommend to use the sql server broker service.

Please read my comment first. And after that if you still want to use timer the problem is in the
C#
int interval = cmd.ExecuteNonQuery();


this method returns rows affected by the query not the data. you should use Reader and then get the column.

SqlCommand.ExecuteNonQuery Method (System.Data.SqlClient)[^]

c# - How to use executeReader() method to retrieve the value of just one cell - Stack Overflow[^]
 
Share this answer
 
Comments
Member 13714562 11-May-18 2:10am    
Thank you so much. It's working. I used cmd.ExecuteScalar().
CS2011 11-May-18 4:18am    
You can execute the query on SSMS and see the result matches the one you are getting. Basically this query will sort the data on IntervalTime and highest value will be returned. So if IntervalTime column has a value 700 already and you insert a value 500 after that it will still return 700.
Your code has one timer and you say you want two timers. The answer seems obvious...

It involves creating a variable you might call 'timer2'.....
 
Share this answer
 
Comments
Member 13714562 11-May-18 0:51am    
Of course I have declared it, as a global variable.

Timer timer1 = new Timer();
Timer timer2 = new Timer();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900