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 3698904
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:02:18+00:00 2026-05-19T05:02:18+00:00

I am writing my first Windows Service using C# and I am having some

  • 0

I am writing my first Windows Service using C# and I am having some trouble with my Timer class.

When the service is started, it runs as expected but the code will not execute again (I want it to run every minute)

Please take a quick look at the attached source and let me know if you see any obvious mistakes!

TIA

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.IO;

namespace CXO001
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

         /*
         * Aim: To calculate and update the Occupancy values for the different Sites
         * 
         * Method: Retrieve data every minute, updating a public value which can be polled
         */

        protected override void OnStart(string[] args)
        {
            Daemon();
        }

        public void Daemon()
        {
            TimerCallback tcb = new TimerCallback(On_Tick);
            TimeSpan duetime = new TimeSpan(0, 0, 1);
            TimeSpan interval = new TimeSpan(0, 1, 0);
            Timer querytimer = new Timer(tcb, null, duetime, interval);
        }

        protected override void OnStop()
        {

        }



        static int[] floorplanids = new int[] { 115, 114, 107, 108 };
        public static List<Record> Records = new List<Record>();
        static bool firstrun = true;

        public static void On_Tick(object timercallback)
        {
            //Update occupancy data for the last minute
            //Save a copy of the public values to HDD with a timestamp

            string starttime;

            if (Records.Count > 0)
            {
                starttime = Records.Last().TS;
                firstrun = false;
            }
            else
            {
                starttime = DateTime.Today.AddHours(7).ToString();
                firstrun = true;
            }

            DateTime endtime = DateTime.Now;
            GetData(starttime, endtime);
        }

        public static void GetData(string starttime, DateTime endtime)
        {
            string connstr = "Data Source = 192.168.1.123; Initial Catalog = Brickstream_OPS; User Id = Brickstream; Password = bstas;";
            DataSet resultds = new DataSet();

            //Get the occupancy for each Zone
            foreach (int zone in floorplanids)
            {
                SQL s = new SQL();
                string querystr = "SELECT SUM(DIRECTIONAL_METRIC.NUM_TO_ENTER - DIRECTIONAL_METRIC.NUM_TO_EXIT) AS 'Occupancy' FROM REPORT_OBJECT INNER JOIN REPORT_OBJ_METRIC ON REPORT_OBJECT.REPORT_OBJ_ID = REPORT_OBJ_METRIC.REPORT_OBJECT_ID INNER JOIN DIRECTIONAL_METRIC ON REPORT_OBJ_METRIC.REP_OBJ_METRIC_ID = DIRECTIONAL_METRIC.REP_OBJ_METRIC_ID WHERE (REPORT_OBJ_METRIC.M_START_TIME BETWEEN '" + starttime + "' AND '" + endtime.ToString() + "') AND (REPORT_OBJECT.FLOORPLAN_ID = '" + zone + "');";
                resultds = s.Go(querystr, connstr, zone.ToString(), resultds);
            }

            List<Record> result = new List<Record>();
            int c = 0;

            foreach (DataTable dt in resultds.Tables)
            {
                Record r = new Record();
                r.TS = DateTime.Now.ToString();
                r.Zone = dt.TableName;
                if (!firstrun)
                {
                    r.Occupancy = (dt.Rows[0].Field<int>("Occupancy")) + (Records[c].Occupancy);
                }
                else
                {
                    r.Occupancy = dt.Rows[0].Field<int>("Occupancy");
                }
                result.Add(r);
                c++;
            }

            Records = result;
            MrWriter();
        }

        public static void MrWriter()
        {
            StringBuilder output = new StringBuilder("Time,Zone,Occupancy\n");

            foreach (Record r in Records)
            {
                output.Append(r.TS);
                output.Append(",");
                output.Append(r.Zone);
                output.Append(",");
                output.Append(r.Occupancy.ToString());
                output.Append("\n");
            }

            output.Append(firstrun.ToString());
            output.Append(DateTime.Now.ToFileTime());

            string filePath = @"C:\temp\CXO.csv";
            File.WriteAllText(filePath, output.ToString());
        }

    }
}
  • 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-19T05:02:18+00:00Added an answer on May 19, 2026 at 5:02 am

    The manual states:

    As long as you are using a Timer, you must keep a reference to it. As with any managed object, a Timer is subject to garbage collection when there are no references to it. The fact that a Timer is still active does not prevent it from being collected.

    Your Timer is probably being collected by the GC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.