简体   繁体   中英

C# Error 1053 the service did not respond to the start or control request in a timely fashion

I realize there are a ton of these posts, but none them, believe it or not, resolve my problem.

I have the following code here that uses the ManagementEventWatcher class to kill a process from another in house app if it runs too long, which it occasionally does and kills the cpu.

Anyway, it gets this error instantaneously when starting the service. Nothing in the event log. Currently I have it testing with notepad.exe.

public AppXKiller()
        {
            InitializeComponent();

            this.ServiceName = "AppXKiller";
            this.EventLog.Log = "Application";

            // These Flags set whether or not to handle that specific
            //  type of event. Set to true if you need it, false otherwise.
            this.CanHandlePowerEvent = true;
            this.CanHandleSessionChangeEvent = true;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;
            this.CanStop = true;

        }

        static void Main()
        {
            ServiceBase.Run(new AppXKiller());
        }

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

        protected override void OnContinue()
        {
            base.OnContinue();
        }

        public void registerWatcher()
        {
            string pol = "2";
            string appName = "notepad.exe";

            string queryString =
                "SELECT *" +
                "  FROM __InstanceOperationEvent " +
                "WITHIN  " + pol +
                " WHERE TargetInstance ISA 'Win32_Process' " +
                "   AND TargetInstance.Name = '" + appName + "'";

            // You could replace the dot by a machine name to watch to that machine
            string scope = @"\\.\root\CIMV2";

            // create the watcher and start to listen
            ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);
            watcher.EventArrived += new EventArrivedEventHandler(this.OnEventArrived);
            watcher.Start();
        }

        private void OnEventArrived(object sender, EventArrivedEventArgs e)
        {
            Thread.Sleep(20000);
            Process[] localByName = Process.GetProcessesByName("notepad");

            if (localByName.Length > 0)
            {
                localByName[0].Kill();
            }
        }

        protected override void OnStop()
        {

        }
    }

Turns out that the application has to be the release version of the build, not the debug version. This makes no sense but oh well. I guess if I want to test and debug the app I have to do it in release mode.

  1. Choose Release from the drop down menu up top (somewhere under tools depending on the size of your screen). It probably says debug.
  2. Build the app.
  3. Install the service from the release folder.

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