3
Answers

C# Service

Matt

Matt

15y
3.3k
1
I am new to writing a windows service, could you please have a look at the following code so you can tell me why the timer_tick event never fires?

Thanks :-)

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.IO;

namespace MyNewService
{
    public partial class MyNewService : ServiceBase
    {

        TextWriter tw = new StreamWriter("log.txt");

        public MyNewService()
        {
            InitializeComponent();
                                 
            tw.WriteLine(DateTime.Now + " COSHH Service has been installed correctly!");
            tw.Close();
        }

        protected override void OnStart(string[] args)
        {
            timer1.Start();
           
        }

        protected override void OnStop()
        {
           timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           
            tw.WriteLine(DateTime.Now + " hello from COSHH Updater!");
            tw.Close();
        }
    }
}

Answers (3)