2011-01-18 5 views
0

나는 이것에 관해 다시 게시하는 것을 싫어하지만 나는 그것을 고치려고 생각하는 자신의 마지막 게시물에 대답했다. 기본적으로 내 C# .NET 응용 프로그램이 종료 될 때 실행 한 Java 프로세스를 제거하려고합니다. 초기 문제는 processID를 정적 클래스 멤버 변수 (분명히 작업을하지 못함)에 저장하려고했기 때문입니다. 글로벌 클래스 예제를 온라인에서 찾았지만 대신 그 프로세스를 종료하지 않았습니다.C# Console App에서 자바 프로세스를 죽이는 것

디버깅이 제대로 작동하지 않습니다. 내가 만든 응용 프로그램을 실행하는 대신 응용 프로그램의 새 인스턴스를 만들고 심지어 "Bin"디렉터리에 작업 디렉터리를 설정해도 작동하지 않는다고 생각합니다. 그래서 지금은 .exe 디렉터리를 Bin 디렉터리에서 실행해야합니다.

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(); 
       } 
      } 
     } 
    } 
} 
+0

코드가'Current_ProcessExit'의'WriteLine'에 도착합니까? – casablanca

+0

일부 Java 응용 프로그램은 정상적으로 종료되지 않습니다 (중지를 거부하는 데몬이 아닌 스레드가있는 경우). 따라서 이것이 작동하지 않는 한 가지 원인이 될 수 있습니다. – biziclop

+0

@biziclop : 해당 진술에 대한 참조를 제공해 주시겠습니까? 나는 호기심이 많다. :) – Davidann

답변

0

이것은) (SetConsoleCtrlHandler를 사용하여 이벤트 AppDomain.CurrentDomain.ProcessExit 잡기로 전환함으로써 해결되었다;

관련 문제