简体   繁体   中英

How to debug OnStop in Windows Service

I have written that is attaching the adding the new task schedular. The code able to register task scheduler on Onstart and the code is working perfectly and able to debug the code. But i want to do some task onStop but i am not able to debug. What is the option and what i need to do to debug the onStop code. Because when i press stop button in visual studio the program stop but not onStop code part can debug.

    protected override void OnStart(string[] args)
            {
                // System.Diagnostics.Debugger.Launch(); 
    
                try
                {

                  // Other code where i am able to debug 
                }
                catch (Exception ie)
                {
                    StreamWriter sw;
                    sw = File.AppendText("D:\\DvrService.txt");
                    sw.WriteLine(ie.Message);
                    sw.Close();
                }
            }


        protected override void OnStop()
          {
            try
            {
                 System.Diagnostics.Debugger.Launch();                

                foreach (string key in System.Configuration.ConfigurationManager.AppSettings)
                {
                    var chromeDriverProcesses = Process.GetProcesses().Where(pr => pr.ProcessName == key.ToUpper()); // without '.exe'

                    foreach (var process in chromeDriverProcesses)
                    {
                        process.Kill();

                        StreamWriter sw;
                        sw = File.AppendText("E:\\DvrService.txt");
                        sw.WriteLine("Trying to kill Service" + key);
                        sw.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                //TODO: Log this error
                StreamWriter sw;
                sw = File.AppendText("E:\\DvrService.txt");
                sw.WriteLine("Trying to kill Service");
                sw.Close();
            }
        }

Program.CS

static void Main()
        {
#if DEBUG
            Service1 myService = new Service1();
            myService.onDebug();
            
   
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

#else

            ServiceBase[] ServicesToRun;
                    ServicesToRun = new ServiceBase[] 
                    { 
                        new Service1() 
                    };
                    ServiceBase.Run(ServicesToRun);

#endif

        }

Pressing the "Stop" button in Visual Studio is not the same as stopping a Windows Service.

If you want to debug OnStop in a Windows Service you have to register the service and have Windows start the service (via GUI or command line). When you register the service make sure that you are pointing to the debug build.

Then, in Visual Studio, attach to the running process, set a breakpoint in OnStop and then, use the GUI or command line to stop the service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM