C# .NET - Windows service not starting - Asked By Austine on 07-Aug-12 08:55 AM

Hi , I have a windows service with a timer that checks for if a program is running or not.

If the program is running , it does not do anything but if not running , it starts the  program from my program files where it was installed on the computer.
 This check is for every 5 seconds.
When i put a break point , It works but on live environment , it does not work.

What am i missing ?.

Here is my code

 public partial class Service1 : ServiceBase
    {
        System.Timers.Timer timer = new System.Timers.Timer();


        public Service1()
        {
            InitializeComponent();


            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Elapsed += new ElapsedEventHandler(OnTimeEvent);
            timer.Interval = 5000;
            timer.Enabled = true;
        }


        protected override void OnStart(string[] args)
        {
          
            timer.AutoReset = true;
            timer.Interval = 5000;
            timer.Enabled = true;
            timer.Start();
        }


        protected override void OnStop()
        {
            timer.AutoReset = false;
            timer.Enabled = false;
        }
        protected void OnTimeEvent(object soucre, ElapsedEventArgs e)
        {
            timer.Stop();


            if (FindAndKillProcess("RegistryKey") == false)
            {
              string file = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\kenteleTicketpopup\\Ticketpopupsetup\\Ticketpopup.lnk";
              System.Diagnostics.Process.Start(file);
            }     
            
            timer.Enabled = true;


        }
        public bool FindAndKillProcess(string name)
        {
            //here we're going to get a list of all running processes on
            //the computer
            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName.StartsWith(name))
                {
                    //clsProcess.Kill();
                    return true;
                }
            }


            return false;
        }
    }
}


Please help to get this up and running.

Am suspecting might be the access to the programfiles on my PC.



Peter Bromberg replied to Austine on 07-Aug-12 05:12 PM
You are creating the timer instance at class level. Then again in the constructor you are creating it all over again, using the same variable.
Annie Calvert replied to Austine on 09-Aug-12 03:09 AM
You should usethe System.Diagnostics.Debugger.Break(); method. If you put this in the beginning of your service it should prompt you to attach Visual Studio to it and you can step through it.
http://www.dapfor.com/en/net-suite/net-grid/tutorial/data-types