CrimsonGT 0 Newbie Poster

I wrote a small c# .NET application that is basically a controller for a Java application. My company has been using a .bat file to launch it, but we need an .exe so our web panel can detect when it is online/offline/etc. When my .NET application starts, it starts the Java app, when my .NET application ends, it SHOULD kill the Java app.

For some reason, it is not killing the Java application. If anyone can take a look at my code and let me know if they see the reason (and also if theres anything I could improve as this is my first C# .NET app)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using CommandLine.Utility;

namespace MinecraftDaemon
{
    class Program
    {
        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 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");
            }
        }

        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();
                }
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.