gpt4 book ai didi

c# - 在 C# 上模拟 CTRL-C 关闭 Java 应用程序

转载 作者:行者123 更新时间:2023-11-30 04:44:43 26 4
gpt4 key购买 nike

我使用 InputSimulator 创建了一些代码,可以从 C# 控制台应用程序正确关闭 java 应用程序,但当我尝试时,它没有给我预期的结果。

当我使用键盘 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 应用程序时

Full thread dump Java HotSpot(TM) Client VM (20.6-b01 mixed mode):

"DestroyJavaVM" prio=6 tid=0x4b524000 nid=0x6f4 waiting on condition [0x00000000 ] java.lang.Thread.State: RUNNABLE

"Thread-1" prio=6 tid=0x4b523800 nid=0x65c waiting on condition [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 runnable [0x4cc7f000] java.lang.Thread.State: RUNNABLE at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method) at 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)

我的输入模拟器使用:

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

我做错了什么?

最佳答案

主页( http://inputsimulator.codeplex.com/ )建议 ctrl-C q 可以通过以下方式完成:

InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);

文档没有说明cancel是什么,但幸运的是JamesB勇敢地搜索了源代码,发现它指的是break。

尝试上面的代码,它应该能够模拟真正的 ctrl-c。

关于c# - 在 C# 上模拟 CTRL-C 关闭 Java 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11314556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com