检测在C#.NET应用程序停机?应用程序、NET

由网友(最羙不過゛初见)分享简介:我写一个小控制台应用程序(将跑作为服务),基本上启动它运行时一个Java应用程序,自行关闭,如果在Java应用程序关闭,并关闭了Java应用程序,如果它关闭。我想我有前两个工作正常,但我不知道如何检测,当.NET应用程序正在关闭,这样我就可以关闭Java应用之前,这种事情发生。谷歌搜索只返回了一堆东西有关检测的Wind...

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

我想我有前两个工作正常,但我不知道如何检测,当.NET应用程序正在关闭,这样我就可以关闭Java应用之前,这种事情发生。谷歌搜索只返回了一堆东西有关检测的Windows关闭。

谁能告诉我怎么可以处理一部分,如果其余的看起来不错?

 命名空间MinecraftDaemon
{
    类节目
    {
        公共静态无效LaunchMinecraft(字符串文件,字符串memoryValue)
        {
            字符串memParams =-Xmx+ memoryValue +M+-Xms+ memoryValue +M;
            字符串的args = memParams +罐子+文件+NOGUI;
            的ProcessStartInfo processInfo =新的ProcessStartInfo(java.exe的,参数);
            processInfo.CreateNoWindow = TRUE;
            processInfo.UseShellExecute = FALSE;

            尝试
            {
                使用(流程minecraftProcess =的Process.Start(processInfo))
                {
                    minecraftProcess.WaitForExit();
                }
            }
            抓住
            {
                //日志错误
            }
        }

        静态无效的主要(字串[] args)
        {
            参数的CommandLine =新的参数(参数);

            如果(的CommandLine [档案] = NULL和放大器;!&安培;!的CommandLine [记忆] = NULL)
            {
                //启动应用程序
                LaunchMinecraft(的CommandLine [档案],命令行[记忆]);
            }
            其他
            {
                LaunchMinecraft(minecraft_server.jar,1024);
            }
        }
    }
}
 

解决方案

您需要在您的主要方法注册此事件:

  Application.ApplicationExit + =新的EventHandler(AppEvents.OnApplicationExit);
 
使用 VS 2017 生成 C .NET Core 应用程序

和添加事件处理

 公共无效OnApplicationExit(对象发件人,EventArgs的)
{
    尝试
    {
        Console.WriteLine(应​​用程序正在关闭。);
    }
    赶上(NotSupportedException异常)
    {
    }
}
 

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");
            }
        }
    }
}

解决方案

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)
    {
    }
}

阅读全文

相关推荐

最新文章