我创建了一些代码来从 C# 控制台应用程序正确关闭 java 应用程序,InputSimulator
但是当我尝试它时,它没有给我预期的结果。
当我使用键盘 CTRL-C 关闭我的 Java 应用程序时
...
[INFO] 2012-07-03 19:29:36 - Packet: [C] 0x02
[INFO] 2012-07-03 19:29:40 - Shutdown hook raised, shutting down...
[INFO] 2012-07-03 19:29:45 - All data saved. Good luck!
当我使用 InputSimulator 关闭我的 java 应用程序时
全线程转储 Java HotSpot(TM) 客户端 VM(20.6-b01 混合模式):
“DestroyJavaVM”prio=6 tid=0x4b524000 nid=0x6f4 等待条件 [0x00000000] java.lang.Thread.State: RUNNABLE
“线程 1”prio=6 tid=0x4b523800 nid=0x65c 等待条件 [0x4cd0f000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at gameserver.utils.DeadlockDetector .run(DeadlockDetector.java:76) at java.lang.Thread.run(Unknown Source)
“server-rdc-acceptor”prio=6 tid=0x4b523000 nid=0x102c 可运行 [0x4cc7f000] java.lang.Thread.State: 在 sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method) 在 sun.nio 处可运行。 ch.WindowsSelectorImpl$SubSelector.poll(Unknown Source) at sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(Unknown Source)
at sun.nio.ch.WindowsSelectorImpl.doSelect(Unknown Source) at sun.nio.ch.SelectorImpl.lockAndDoSelect(Unknown Source) - locked <0x3e057ef0> (a sun.nio.ch.Util$2) - locked <0x3e057ee0> (a java.util.Collections$UnmodifiableSet) - locked <0x3e057cc0> (a sun.nio.ch.WindowsSelectorImpl) at sun.nio.ch.SelectorImpl.select(Unknown Source) at sun.nio.ch.SelectorImpl.select(Unknown Source) at commons.ngen.network.Acceptor.run(Acceptor.java:259)
我的 InputSimulator 使用:
private void StartServer()
{
ProcessStartInfo info = new ProcessStartInfo();
info.UseShellExecute = false;
info.FileName = "java"
info.Arguments = "-Xms512m -Xmx1024m server.jar gameserver.Server"
ServerProcess = new Process();
ServerProcess.StartInfo = info;
ServerProcess.Start();
Thread.Sleep(60000);
CloseCorrectly(ServerProcess);
}
private void CloseCorrectly(Process pr)
{
IntPtr hWnd = pr.MainWindowHandle;
if (hWnd != null)
{
SetForegroundWindow(hWnd);
Thread.Sleep(1000);
InputSimulator f = new InputSimulator();
f.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.CANCEL);
Thread.Sleep(1000);
}
}
我做错了什么?