CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2016
    Posts
    1

    Question C# Windows Service With Timer Not Sending Data

    Hi all. I made a Windows service that posts performance data to a mySQL database using a json string. However, this service is required to post data every minute. I have added a timer but, it doesn't seem to send the data to the database every minute.

    I have 2 classes in my project. The program class initiates the service and runs the performance code.

    Code:
    public static string ServiceName = "performanceService";
    
        static void Main(string[] args)
        {
            if (!Environment.UserInteractive)
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                new ServiceControl()
                };
                ServiceBase.Run(ServicesToRun);
    
    
            }
            else
            {
                PerformanceCounter ramCount = new PerformanceCounter("Memory", "Available MBytes");
                PerformanceCounter cpuCount = new PerformanceCounter("Processor", "% Processor Time", "_Total");
    Console.WriteLine("Press any key to view other information...\n");
                    Console.WriteLine("CPU and RAM information");
                    while (!Console.KeyAvailable)
                    {
                        double perf = cpuCount.NextValue();
                        Console.WriteLine("CPU Performance: " + perf + " %");
                        ServerStats.cpuLoad = perf;
    
    
                        double ram = ramCount.NextValue();
                        Console.WriteLine("RAM available: " + ram + " MB");
                        ServerStats.memoryLoad = ram;
    
                        Thread.Sleep(1000);
    Remaining code contains more code that executes performance.

    Then I have a ServiceControl class, which in fact is a Windows Service. And in this class I have setup my timer.

    Code:
    System.Timers.Timer timer = new System.Timers.Timer();
    
        public ServiceControl()
        {
            ServiceName = Program.ServiceName;
            InitializeComponent();
    
        }
    
        public void timerElapsed(object sender, EventArgs e)
        {
            Console.WriteLine("Time Elapsed");
        }
        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            timer.Elapsed += new ElapsedEventHandler(timerElapsed);
            timer.Interval = 60000;
            timer.Enabled = true;
            timer.Start();
    
        }
        protected override void OnStop()
        {
            base.OnStop();
            timer.Enabled = false;
            timer.Stop();
        }
    private void InitializeComponent()
        {
            // 
            // ServiceControl
            // 
            this.ServiceName = "performanceService";
    
        }
    If there is any additional code that you would like to see, please let me know. It only posts it when the application is run, rather when the service is running. I do not get any errors. I would like the service to post info every minute, without running the application. Am I missing code in the Main() or in the timerElapsed()?
    Is there anything wrong within my code that doesn't allow the service to post the data at the specified interval?
    Thank you.

  2. #2
    Join Date
    May 2002
    Posts
    511

    Re: C# Windows Service With Timer Not Sending Data

    So it is only posting once, correct? If so this is because you only started the timer once and it completed its task. So in your timerElapsed() method you need to either reset the existing timer or create a new one.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# Windows Service With Timer Not Sending Data

    There seems to be a difference between the code that is being run when you are debugging and the code that gets run as the service.

    When run as a service, you didn't include the code that executes the PerformanceCounter. Has this code been written? If it has, hopefully the code you run as a console app isn't a copy of the other code.

  4. #4
    Join Date
    Jan 2016
    Posts
    2

    Re: C# Windows Service With Timer Not Sending Data

    This link tells you how to create a window service and setup a timer in the service (https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured