如果用户从任务管理器杀死进程,如何再次启动我的c#应用程序



我用c#开发了一个应用程序。如果用户在任务管理器中杀死我的应用程序的进程,那么应用程序是否会自动重新启动我搜索了很多这种类型的事件,当进程将从任务管理器手动终止时,应该触发这些事件由于

如果用户终止了您的进程-那就差不多了。你不会得到任何事件,什么都没有。

您需要做的是让第二个进程运行,监视第一个进程,偶尔轮询正在运行的进程列表,并在第一个进程停止的情况下重新启动它。或者,您可以让它们使用IPC来执行偶尔的心跳,以避免查看整个进程列表。

当然,如果用户先杀死了监视器进程,那么除非两个进程互相监视并启动丢失的进程,否则你不会真正取得任何进展,但现在你只是在原地打转。

一般来说,这是一个坏主意。如果用户想要停止您的进程,您应该让他们停止。你为什么要阻止他们?

我看到的唯一解决方案是另一个进程监视主进程并重新启动它。我会在主进程中使用互斥锁,然后在观察进程中观察这个互斥锁。释放互斥锁意味着主进程已经停止。

/// <summary>
/// Main Program.
/// </summary>
class Program
{
    static void Main(string[] args)
    {
        // Create a Mutex which so the watcher Process 
        using (var StartStopHandle = new Mutex(true, "MyApplication.exe"))
        {
            // Start the Watch process here. 
            Process.Start("MyWatchApplication.exe");                
            // Your Program Code...
        }
    }
}

在观看过程中:

/// <summary>
/// Watching Process to restart the application.
/// </summary>
class Programm
{
    static void Main(string[] args)
    {
        // Create a Mutex which so the watcher Process 
        using (var StartStopHandle = new Mutex(true, "MyApplication.exe"))
        {
            // Try to get Mutex ownership. 
            if (StartStopHandle.WaitOne())
            { 
                // Start the Watch process here
                Process.Start("MyApplication.exe");
                // Quit after starting the Application.
            }
        }
    }
}

相关内容

最新更新