private void Initialize(string pipeName, bool client)
        {
            PipeName = pipeName;
            Client   = client;

            Deactivated = true;

            Pipe              = new NamedPipe(pipeName, !client);
            Pipe.OnConnected += () =>
            {
                Deactivated = false;

                var handler = OnPipeConnected;
                if (handler != null)
                {
                    handler();
                }
            };

            if (client)
            {
                Pipe.Connect();
            }
            else
            {
                _ = Pipe.TryAccept();
            }
        }
Exemplo n.º 2
0
        private bool RunApp(string exe, string[] args, string cwd = ".", string taskName = "", Action o = null, bool cleanConsole = true)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                if (cleanConsole)
                {
                    textBox1.Clear();
                }
                Log(taskName, LogLevel.Verbose);
            }));

            NamedPipe mLogPipe;
            Thread    mOutputThread = null;

            AppRun?.Invoke();

            SetText(taskName);

            Process process = new Process();

            process.StartInfo.FileName               = exe;
            process.StartInfo.Arguments              = string.Join(" ", args);
            process.StartInfo.WorkingDirectory       = cwd;
            process.StartInfo.UseShellExecute        = false;
            process.StartInfo.CreateNoWindow         = true;
            process.StartInfo.RedirectStandardError  = false;
            process.StartInfo.RedirectStandardOutput = false;
            process.StartInfo.RedirectStandardInput  = true;
            process.EnableRaisingEvents              = true;

            process.Start();
            runningProcesses.Add(process);
            SetText(taskName);


            mLogPipe = new NamedPipe();
            if (mLogPipe.Connect(process))
            {
                if (mOutputThread != null && mOutputThread.IsAlive)
                {
                    mOutputThread.Abort();
                }
                mOutputThread = new Thread(() => {
                    Debug.WriteLine("Pipe open");
                    try
                    {
                        var level = LogLevel.Info;
                        while (process != null && !process.HasExited)
                        {
                            string text = mLogPipe.Read();
                            //Debug.WriteLine(text);

                            if (text.StartsWith("`~[~`")) // control code
                            {
                                var raw  = text.Split('`');
                                var code = raw[raw.Length - 1].Trim();

                                switch (code)
                                {
                                case "Reset":
                                    level = LogLevel.Info;
                                    break;

                                case "1111":
                                    level = LogLevel.Info;
                                    break;

                                case "1101":
                                    level = LogLevel.Warn;
                                    break;

                                case "1001":
                                    level = LogLevel.Error;
                                    break;

                                case "0101":
                                    level = LogLevel.Success;
                                    break;

                                default:
                                    Debug.WriteLine("Code not implemented: " + code);
                                    break;
                                }
                            }
                            else if (text.Length > 0)
                            {
                                Log(text.Replace("\r\n", ""), level);
                            }
                        }
                    }
                    catch (ThreadAbortException)
                    {
                    }
                    catch (Exception)
                    {
                    }
                    Debug.WriteLine("Pipe close");
                    Log("Process exited with exit code: " + process.ExitCode, process.ExitCode != 0 ? LogLevel.Warn : LogLevel.Verbose);
                });
                mOutputThread.Start();
            }

            while (!process.HasExited && !isCancelling)
            {
                Thread.Sleep(300);
            }

            Meme.StopElevatorMusic();

            if (runningProcesses.Contains(process))
            {
                runningProcesses.Remove(process);
            }

            this.Invoke(new MethodInvoker(() =>
            {
                if (runningProcesses.Count == 0)
                {
                    mButton3.Visible = false;
                }
            }));

            AppClose?.Invoke(process.ExitCode);

            if (runningProcesses.Count == 0)
            {
                SetText(null);
            }

            this.Invoke(new MethodInvoker(() =>
            {
                MainWindow.Instance.ToggleConsole(false);
            }));

            return(process.ExitCode == 0);
        }