Domanda

I founded some code to correctly shutdown java application from C# console application, using InputSimulator but when i tried it, it didnt give me an expected result.

When i close my java application using keyboard CTRL-C

...
[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!

When i close my java application using InputSimulator

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)

My InputSimulator using:

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

What i'm doing wrong?

È stato utile?

Soluzione

The homepage (http://inputsimulator.codeplex.com/) suggests that ctrl-C qould be done via:

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

The docs don't suggest what cancel is but fortunately JamesB has bravely searched the source to find that it refers to break.

Try the above code and that should hopefully simulate a real ctrl-c.

Altri suggerimenti

I understand you are already using InputSimulator and the question is for SimulateModifiedKeyStroke

But you can achieve this by calling

Sendkeys.SendWait("^c")
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top