从 C# 控制台应用程序终止 Java 进程
我讨厌再次发帖讨论这个问题,但我回答了我自己的上一篇文章,认为我已经修复了它(但我没有)。基本上,当我的 c# .NET 应用程序关闭时,我想删除它创建的正在运行的 Java 进程。最初的问题是我试图将 processID 保存到静态类成员变量(这显然不起作用)。我在网上找到了一个全局类示例并使用了它,但它仍然没有关闭该过程。
调试后无法正常工作。我猜它只是创建应用程序的一个新实例,而不是运行我构建的实例,甚至将工作目录设置为“Bin”目录也不起作用。所以我现在只需要从 Bin 目录运行我的 .exe 即可。
namespace MinecraftDaemon
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting Minecraft Daemon...");
Arguments CommandLine = new Arguments(args);
// Hook ProcessExit Event
AppDomain.CurrentDomain.ProcessExit += new EventHandler(Current_ProcessExit);
if (CommandLine["file"] != null && CommandLine["memory"] != null)
{
// Launch the Application (Command Line Parameters)
LaunchMinecraft(CommandLine["file"], CommandLine["memory"]);
}
else
{
// Launch the Application (Default Parameters)
LaunchMinecraft("minecraft_server.jar", "1024");
}
}
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))
{
GlobalClass.ProcessID = minecraftProcess.Id;
Console.WriteLine("Process ID is " + GlobalClass.ProcessID);
minecraftProcess.WaitForExit();
}
}
catch
{
// Log Error
}
}
static void Current_ProcessExit(object sender, EventArgs e)
{
// Loop the Current Windows Processes
foreach (Process winProcess in Process.GetProcesses())
{
Console.WriteLine("WinProcessID is " + winProcess.Id + " GlobalClass.ProcessID is " + GlobalClass.ProcessID);
// If this is our Process, shut it down
if (winProcess.Id == GlobalClass.ProcessID)
{
Process.GetProcessById(GlobalClass.ProcessID).Kill();
}
}
}
}
}
I hate to post about this again but I answered my own last post thinking I fixed it (which I didn't). Basically when my c# .NET application shuts down, I want to remove the running Java process that it created. The initial problem was that I was trying to save processID to a static class member variable (which obviously didnt work). I found a Global Class example online and used that instead, however it still isn't shutting down the process.
Debugging it isn't working properly. I guess it just creates a new instance of the application rather than running the one that I built, and even setting the working directory to the "Bin" directory doesn't work. So I am just having to run my .exe from the Bin directory at the moment.
namespace MinecraftDaemon
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting Minecraft Daemon...");
Arguments CommandLine = new Arguments(args);
// Hook ProcessExit Event
AppDomain.CurrentDomain.ProcessExit += new EventHandler(Current_ProcessExit);
if (CommandLine["file"] != null && CommandLine["memory"] != null)
{
// Launch the Application (Command Line Parameters)
LaunchMinecraft(CommandLine["file"], CommandLine["memory"]);
}
else
{
// Launch the Application (Default Parameters)
LaunchMinecraft("minecraft_server.jar", "1024");
}
}
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))
{
GlobalClass.ProcessID = minecraftProcess.Id;
Console.WriteLine("Process ID is " + GlobalClass.ProcessID);
minecraftProcess.WaitForExit();
}
}
catch
{
// Log Error
}
}
static void Current_ProcessExit(object sender, EventArgs e)
{
// Loop the Current Windows Processes
foreach (Process winProcess in Process.GetProcesses())
{
Console.WriteLine("WinProcessID is " + winProcess.Id + " GlobalClass.ProcessID is " + GlobalClass.ProcessID);
// If this is our Process, shut it down
if (winProcess.Id == GlobalClass.ProcessID)
{
Process.GetProcessById(GlobalClass.ProcessID).Kill();
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过从捕获事件 AppDomain.CurrentDomain.ProcessExit 切换到使用 SetConsoleCtrlHandler() 解决了这个问题;
This was resolved by switching from catching the Event AppDomain.CurrentDomain.ProcessExit to using SetConsoleCtrlHandler();