【问题标题】:C# Kill process before computer enters sleep modeC#在计算机进入睡眠模式之前杀死进程
【发布时间】:2018-06-14 01:45:58
【问题描述】:

在 Windows 10 中,我创建了一个在启动时初始化的在后台运行的进程。当计算机进入睡眠状态时,它会使 Windows 崩溃并给我一个 BSOD。

我愿意接受任何解决方案,但是我目前正试图在“暂停”PowerModeChanged 事件发生时终止该进程。在机器进入休眠状态之前,这似乎不足以杀死进程,并且机器仍在崩溃。我的 PowerModeChanged 监听器肯定在工作,它肯定是导致机器崩溃的辅助进程。

我对后台进程开发有点陌生,我整天都在尝试不同的方法,但进展不大。肯定有人必须有这方面的经验并且知道解决方法。

// Application path and command line arguments
    static string ApplicationPath = @"C:\path\to\program.exe";
    static Process ProcessObj = new Process();

    static void Main(string[] args)
    {
        SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);

        startProcess(); 
        Console.ReadKey(); 
    }

    static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
    {
        Console.WriteLine(e.Mode.ToString());
        if (e.Mode == PowerModes.Suspend)
        {
            ProcessObj.Kill(); 
        }

        if (e.Mode == PowerModes.Resume)
        {
            startProcess();
        }
    }

    static void startProcess()
    {

        // Create a new process object
        try
        {
            // StartInfo contains the startup information of the new process
            ProcessObj.StartInfo.FileName = ApplicationPath;

            // These two optional flags ensure that no DOS window appears
            ProcessObj.StartInfo.UseShellExecute = false;
            ProcessObj.StartInfo.CreateNoWindow = true;

            // This ensures that you get the output from the DOS application
            ProcessObj.StartInfo.RedirectStandardOutput = true;

            // Start the process
            ProcessObj.Start();

            // Wait that the process exits
            ProcessObj.WaitForExit();

            // Now read the output of the DOS application
            string Result = ProcessObj.StandardOutput.ReadToEnd();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

【问题讨论】:

  • 虽然你说这是一个“后台进程”,但看起来它实际上是作为前台进程运行的。请说明您如何在启动时启动此程序。换句话说,你如何导致Main() 方法被调用?
  • 我认为 op 意味着静默应用程序,而不是 Windows 服务或较低级别的系统进程。虽然 StartProcess 或 ProcessStart 是 windows 服务的入口点。
  • 用这个链接戳电脑眼睛-blog.backslasher.net/windows-awake-ps.html
  • 噢!过了一分钟,OnStart() 是 Service 派生的重载。
  • 听起来我在最初的方法中找错了树,但为了回答你的问题,我有 2 个程序,一个运行我放置在 /Startup/ 目录中的代码。另一个程序是代码示例中的 ApplicationPath 变量示例的辅助应用程序。我正在更新它以采用 Windows 服务路线。谢谢!

标签: c# windows-10 background-process sleep-mode


【解决方案1】:

在 Windows 操作系统平台上,当一个人真正想要为整个运行的机器运行一个“后台进程”并让该进程在电源事件(例如休眠)后仍然存在时,他们通常将其进程构建为 Windows服务。

许多流行的应用程序都是作为 Windows 服务实现的,例如 Microsoft SQL Server 或 Window 自己的 Web 服务器 (W3SVC)。

可以通过选择创建“Windows 服务”类型的新项目在 Visual Studio 中构建 Windows 服务。

使用此技术,您将能够响应多个事件,包括以下事件(在 System.ServiceProcess.ServiceBase 中定义):

  • OnStart(string[] args)
  • OnStop()
  • OnPause()
  • OnContinue()
  • OnPowerEvent(PowerBroadcastStatus powerStatus)
  • OnShutdown()

您可以在此处找到有关构建 .NET Windows 服务的更多信息:https://docs.microsoft.com/en-us/dotnet/framework/windows-services/

【讨论】:

    猜你喜欢
    • 2015-06-11
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2011-09-13
    • 2020-07-26
    • 1970-01-01
    • 2023-02-01
    相关资源
    最近更新 更多