0

我的想法用完了,找不到任何关于它的参考,所以我开始了......我需要保留一个与我的 c# 项目无关的辅助应用程序始终在后台运行。因此,如果此辅助应用程序崩溃或其他人手动关闭它,它将通过我的 c# 应用程序自动重新启动。

我不知道如何完成这个,我的意思是检查这个应用程序是否被我的 c# 应用程序外部的东西关闭。

任何帮助将不胜感激。提前致谢。

4

5 回答 5

2

下面的代码在 C# 中,它在 WinForm 中。

    private void Form1_Load(object sender, EventArgs e)
    {
        Process p = Process.GetProcessesByName("Notepad")[0];
        p.EnableRaisingEvents = true;
        p.Exited += new EventHandler(p_Exited);
    }

    void p_Exited(object sender, EventArgs e)
    {
        MessageBox.Show("Exit");
    }

It looks for a Process with Name Notepad & retrieved the first instance of it. It sets EnableRaisingEvents to true on it and hooks to the Exited event. Whenever notepad is closed it would display an alert.

Based on this logic, you can build your app.

于 2012-05-18T07:53:35.210 回答
1

作为一种解决方案,您可以使用调用您始终运行的应用程序的 Windows 服务。
您可以使该服务从应用程序捕获错误返回代码并根据错误重新启动它。

于 2012-05-18T07:12:46.200 回答
1

您可以继续检查进程是否正在运行或未使用 vb.net 中的进程类

For Each p As Process In Process.GetProcessesByName("communicator") ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_NORMAL) Next p

如果您想要的进程不在列表中,您可以再次启动它。阿什·库马尔

于 2012-05-18T07:19:42.877 回答
1

最简单的方法是运行计时器并在滴答事件中使用 -

if (Process.GetProcessesByName("communicator").Count() == 0)
{
   Process.Start("communicator.exe");
}
于 2012-05-18T07:49:48.347 回答
0

您可以使用FileSystemWatcher来监视其他应用程序修改的文件。

FileSystemWatcher 具有 Changed、Created、Renamed、Deleted 等事件,可以订阅这些事件以跟踪文件更改。

于 2012-05-18T07:05:16.857 回答