【问题标题】:C# Error 1053 the service did not respond to the start or control request in a timely fashionC# 错误 1053 服务没有及时响应启动或控制请求
【发布时间】:2015-12-28 23:39:52
【问题描述】:

我意识到有很多这样的帖子,但不管你信不信,它们都没有解决我的问题。

我在这里有以下代码,如果它运行时间过长,它使用 ManagementEventWatcher 类从另一个内部应用程序中杀死一个进程,它偶尔会这样做并杀死 cpu。

无论如何,它在启动服务时会立即收到此错误。事件日志中没有任何内容。目前我用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()
        {

        }
    }

【问题讨论】:

    标签: c# .net service wmi


    【解决方案1】:

    原来应用程序必须是构建的发布版本,而不是调试版本。这没有任何意义,但好吧。我想如果我想测试和调试应用程序,我必须在发布模式下进行。

    1. 从顶部的下拉菜单中选择“释放”(根据屏幕大小,在工具下方的某个位置)。可能是调试。
    2. 构建应用程序。
    3. 从发布文件夹安装服务。

    【讨论】:

      猜你喜欢
      • 2011-03-28
      • 2014-08-05
      • 2011-01-05
      • 2011-12-29
      • 2023-03-23
      • 1970-01-01
      • 2016-03-22
      • 2013-12-20
      相关资源
      最近更新 更多