检测 C# NET 中的应用程序关闭?
我正在编写一个小型控制台应用程序(将作为服务运行),它基本上在运行时启动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要在 Main 方法中注册此事件:
并添加事件处理程序
You will need to register this event in your Main method:
and add the event handler
啊 MineCraft :)
由于您的控制台应用程序最终将成为 Windows 服务,请查看 OnStop、OnPowerEvent、onPause 和 onShutDown 方法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.
您需要将事件处理程序添加到 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