我正在编写一个小型控制台应用程序(将作为服务运行),它基本上在运行时启动 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");
}
}
}
}
您需要在 Main 方法中注册此事件:
Application.ApplicationExit += new EventHandler(AppEvents.OnApplicationExit);
并添加事件处理程序
public void OnApplicationExit(object sender, EventArgs e)
{
try
{
Console.WriteLine("The application is shutting down.");
}
catch(NotSupportedException)
{
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)