Пример #1
0
        void RunThreadProc()
        {
LABEL_RESTART:
            StartEvent.Reset();
            try
            {
                string cmdLine = this.CmdLine;

                if (this.Next_TestWeb)
                {
                    cmdLine = cmdLine.Replace(" Test", " TestWeb");
                }

                Console.WriteLine($"DebugHost: Starting the child process '{cmdLine}' ...");
                Console.WriteLine("DebugHost: --------------------------------------------------------------");

                using (Process p = Utils.ExecProcess(cmdLine))
                {
                    CurrentRunningProcessId = p.Id;

                    StatusManager.SetStatus(Status.Running);

                    try
                    {
                        p.WaitForExit();
                    }
                    finally
                    {
                        CurrentRunningProcessId = 0;
                    }

                    Console.WriteLine("DebugHost: --------------------------------------------------------------");
                    Console.WriteLine($"DebugHost: Exit code of the process: {p.ExitCode}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DebugHost: --------------------------------------------------------------");
                Console.WriteLine($"DebugHost: {ex.ToString()}");
                Console.WriteLine("DebugHost: --------------------------------------------------------------");
            }

            StatusManager.SetStatus(Status.Stopped);

            Console.WriteLine("DebugHost: The process is stopped. Waiting for next start command ...");

            if (StartEvent.WaitOne(0) == false)
            {
                CancellationTokenSource cancelSource = new CancellationTokenSource();

                Thread waitKeyThread = new Thread(WaitKeyThreadProc);
                waitKeyThread.Start(cancelSource.Token);

                StartEvent.WaitOne();

                cancelSource.Cancel();
                waitKeyThread.Join();
            }

            goto LABEL_RESTART;
        }
Пример #2
0
        static int Main(string[] args)
        {
            if (args.Length == 1 && args[0].ToLower() == "wait")
            {
                while (true)
                {
                    Console.Write("'r' to restart / 'q' to terminate / 'w' for Auto Razor Recompile>");
                    string line = Console.ReadLine().Trim();
                    if (string.Equals(line, "r", StringComparison.OrdinalIgnoreCase))
                    {
                        return(0);
                    }
                    if (string.Equals(line, "w", StringComparison.OrdinalIgnoreCase))
                    {
                        return(1);
                    }
                    if (string.Equals(line, "q", StringComparison.OrdinalIgnoreCase))
                    {
                        return(4);
                    }
                }
            }

            Utils.DivideCommandLine(Environment.CommandLine, out _, out string argString);

            argString = argString.Trim();

            bool showHelp = false;

            if (string.IsNullOrWhiteSpace(argString) == false)
            {
                Utils.GetKeyAndValue(argString, out string action, out string appIdAndCmdLine);
                Utils.GetKeyAndValue(appIdAndCmdLine, out string appId, out string cmdLine);

                if (string.IsNullOrWhiteSpace(appId) == false)
                {
                    StatusManager.SetTitleBase(appId);
                }

                if (string.IsNullOrWhiteSpace(argString) || string.IsNullOrWhiteSpace(appId))
                {
                    showHelp = true;
                }
                else
                {
                    string appIdHash = Utils.ByteToHex(Utils.HashSHA1(Encoding.UTF8.GetBytes(appId.ToLower() + ":HashDebugHost")), "").ToLower();

                    string pipeName = "pipe_" + appIdHash;

                    string mutexName = "mutex_" + appIdHash;

                    Mode mode = Mode.Unknown;
                    Cmd  cmd  = default;

                    switch (action.ToLower())
                    {
                    case "run":
                        if (string.IsNullOrWhiteSpace(cmdLine) == false)
                        {
                            mode = Mode.Run;
                        }
                        break;

                    case "stop":
                        mode = Mode.Stop;
                        cmd  = Cmd.Stop;
                        break;

                    case "start":
                        mode = Mode.Start;
                        cmd  = Cmd.Start;
                        break;

                    case "restart":
                        mode = Mode.Restart;
                        cmd  = Cmd.Restart;
                        break;
                    }

                    if (mode == Mode.Unknown)
                    {
                        showHelp = true;
                    }
                    else
                    {
                        if (mode == Mode.Run)
                        {
                            GlobalMutex mutex = new GlobalMutex(mutexName);

                            if (mutex.TryLock())
                            {
                                Server svr = new Server(cmdLine, pipeName);

                                mutex.Unlock();
                            }
                            else
                            {
                                Console.WriteLine("Error: Another instance is already running.");

                                return(-2);
                            }
                        }
                        else
                        {
                            Client.ExecuteCommand(cmdLine, pipeName, appId, cmd);
                        }
                    }
                }
            }
            else
            {
                showHelp = true;
            }

            if (showHelp)
            {
                Console.WriteLine(@"Usage:
 DebugHost run <AppId> <CommandLine...>
 DebugHost start <AppId> [CommandLine...]
 DebugHost stop <AppId>
 DebugHost restart <AppId>
");

                return(-1);
            }

            return(0);
        }