检测 C# NET 中的应用程序关闭?

发布于 2024-10-13 02:59:11 字数 1506 浏览 4 评论 0原文

我正在编写一个小型控制台应用程序(将作为服务运行),它基本上在运行时启动 Java 应用程序,如果 Java 应用程序关闭则自行关闭,如果 Java 应用程序关闭则关闭 Java 应用程序。

我认为前两个工作正常,但我不知道如何检测 .NET 应用程序何时关闭,以便我可以在发生这种情况之前关闭 Java 应用程序。 Google 搜索仅返回一堆有关检测 Windows 关闭的信息。

谁能告诉我如何处理该部分以及其余部分是否看起来不错?

namespace MinecraftDaemon
{
    class Program
    {
        public static void LaunchMinecraft(String file, String memoryValue)
        {
            String memParams = "-Xmx" + memoryValue + "M" + " -Xms" + memoryValue + "M ";
            String args = memParams + "-jar " + file + " nogui";
            ProcessStartInfo processInfo = new ProcessStartInfo("java.exe", args);
            processInfo.CreateNoWindow = true;
            processInfo.UseShellExecute = false;

            try
            {
                using (Process minecraftProcess = Process.Start(processInfo))
                {
                    minecraftProcess.WaitForExit();
                }
            }
            catch
            {
                // Log Error
            }
        }

        static void Main(string[] args)
        {
            Arguments CommandLine = new Arguments(args);

            if (CommandLine["file"] != null && CommandLine["memory"] != null)
            {
                // Launch the Application
                LaunchMinecraft(CommandLine["file"], CommandLine["memory"]);
            }
            else
            {
                LaunchMinecraft("minecraft_server.jar", "1024");
            }
        }
    }
}

I am writing a small console application (will be ran as a service) that basically starts a Java app when it is running, shuts itself down if the Java app closes, and shuts down the Java app if it closes.

I think I have the first two working properly, but I don't know how to detect when the .NET application is shutting down so that I can shutdown the Java app prior to that happening. Google search just returns a bunch of stuff about detecting Windows shutting down.

Can anyone tell me how I can handle that part and if the rest looks fine?

namespace MinecraftDaemon
{
    class Program
    {
        public static void LaunchMinecraft(String file, String memoryValue)
        {
            String memParams = "-Xmx" + memoryValue + "M" + " -Xms" + memoryValue + "M ";
            String args = memParams + "-jar " + file + " nogui";
            ProcessStartInfo processInfo = new ProcessStartInfo("java.exe", args);
            processInfo.CreateNoWindow = true;
            processInfo.UseShellExecute = false;

            try
            {
                using (Process minecraftProcess = Process.Start(processInfo))
                {
                    minecraftProcess.WaitForExit();
                }
            }
            catch
            {
                // Log Error
            }
        }

        static void Main(string[] args)
        {
            Arguments CommandLine = new Arguments(args);

            if (CommandLine["file"] != null && CommandLine["memory"] != null)
            {
                // Launch the Application
                LaunchMinecraft(CommandLine["file"], CommandLine["memory"]);
            }
            else
            {
                LaunchMinecraft("minecraft_server.jar", "1024");
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦醒时光 2024-10-20 02:59:11

您需要在 Main 方法中注册此事件:

Application.ApplicationExit += new EventHandler(AppEvents.OnApplicationExit);

并添加事件处理程序

public void OnApplicationExit(object sender, EventArgs e)
{
    try
    {
        Console.WriteLine("The application is shutting down.");
    }
    catch(NotSupportedException)
    {
    }
}

You will need to register this event in your Main method:

Application.ApplicationExit += new EventHandler(AppEvents.OnApplicationExit);

and add the event handler

public void OnApplicationExit(object sender, EventArgs e)
{
    try
    {
        Console.WriteLine("The application is shutting down.");
    }
    catch(NotSupportedException)
    {
    }
}
魂归处 2024-10-20 02:59:11

啊 MineCraft :)

由于您的控制台应用程序最终将成为 Windows 服务,请查看 OnStopOnPowerEvent、onPauseonShutDown 方法msdn.microsoft.com/en-us/library/5xh5cfw0.aspx" rel="nofollow">ServiceBase 类。

Ahh MineCraft :)

Since your Console App will eventually become a windows service, look into OnStop, OnPowerEvent, onPause and onShutDown methods of the ServiceBase class.

别再吹冷风 2024-10-20 02:59:11

您需要将事件处理程序添加到 Application.ApplicationExit 事件。

You'll want to add an event handler to the Application.ApplicationExit event.

你说它将作为服务运行。

在这种情况下,将调用 ServiceBase 类的受保护方法 OnStop()。

http://msdn。 microsoft.com/en-us/library/system.serviceprocess.servicebase.onstop(v=VS.85).aspx

You said it will run as a service.

In that case protected method OnStop() of ServiceBase class will be called.

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstop(v=VS.85).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文