簡體   English   中英

C#錯誤1053,服務未及時響應啟動或控制請求

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

我意識到有很多這樣的帖子,但不管您信不信,都可以解決我的問題。

我這里有以下代碼,如果運行時間太長,則使用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()
        {

        }
    }

原來,該應用程序必須是內部版本,而不是調試版本。 這沒有任何意義,但是很好。 我想我是否要測試和調試應用程序,必須在發布模式下進行。

  1. 從頂部的下拉菜單中選擇“釋放”(取決於屏幕的大小,在工具下的某個位置)。 它可能說調試。
  2. 生成應用。
  3. 從發布文件夾安裝服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM