Beispiel #1
0
        private void doStuff()
        {
            string killFile = Helper.GetCurrDir() + "\\delete-me to kill.txt";

            if (Configuration.Instance.RunProgramName.Length > 0)
            {
                try
                {
                    System.IO.File.WriteAllText(killFile, "Delete-me to kill the process. It will try to gracefully stop for 5 seconds");
                }
                catch { }
                try
                {
                    if (!System.IO.File.Exists(killFile))
                    {
                        killFile = null;
                    }
                }
                catch { }
                using (var outputStream = System.IO.File.AppendText(Helper.GetCurrDir() + "\\output.txt"))
                {
                    outputStream.WriteLine(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + " Initializing...");
                    outputStream.WriteLine(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + " Program: " + Configuration.Instance.RunProgramName);
                    outputStream.Flush();

                    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
                    psi.UseShellExecute = false;
                    psi.CreateNoWindow  = true;
                    psi.Arguments       = Configuration.Instance.RunProgramParams;
                    psi.FileName        = Configuration.Instance.RunProgramName;
                    psi.WindowStyle     = System.Diagnostics.ProcessWindowStyle.Hidden;
                    if (Configuration.Instance.RunProgramPath != null && Configuration.Instance.RunProgramPath.Length > 0)
                    {
                        psi.WorkingDirectory = Configuration.Instance.RunProgramPath;
                    }
                    if (Configuration.Instance.GracefullyCloseCommand != null && Configuration.Instance.GracefullyCloseCommand.Length > 0)
                    {
                        psi.RedirectStandardInput = true;
                    }
                    psi.RedirectStandardOutput = true;

                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo           = psi;
                    p.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler((sender, e) =>
                    {
                        // Prepend line numbers to each line of the output.
                        if (!String.IsNullOrEmpty(e.Data))
                        {
                            outputStream.WriteLine(e.Data);
                            outputStream.Flush();
                        }
                    });
                    p.Start();
                    running = true;
                    p.BeginOutputReadLine();
                    while (running || p.HasExited)
                    {
                        System.Threading.Thread.Sleep(250);
                        if (killFile != null)
                        {
                            if (!System.IO.File.Exists(killFile))
                            {
                                outputStream.WriteLine(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + " ==Kill by file");
                                break;
                            }
                        }
                    }

                    if (!p.HasExited && Configuration.Instance.GracefullyCloseCommand != null && Configuration.Instance.GracefullyCloseCommand.Length > 0)
                    {
                        try
                        {
                            p.StandardInput.WriteLine(Configuration.Instance.GracefullyCloseCommand);
                            outputStream.WriteLine(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + " ==Stop signal sent");
                        }
                        catch (Exception ex) { outputStream.WriteLine(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + " ==Stop signal FAILED to sent: " + ex.Message); }

                        int seconds = Configuration.Instance.GracefullyCloseTimeoutSeconds;
                        if (seconds < 0)
                        {
                            seconds = 5;
                        }
                        if (seconds > 30)
                        {
                            seconds = 30;
                        }
                        for (int i = 0; i < seconds * 10; i++)
                        {
                            System.Threading.Thread.Sleep(100);
                            if (p.HasExited)
                            {
                                break;
                            }
                        }
                    }

                    if (!p.HasExited)
                    {
                        p.Kill();
                    }
                    p.Close();
                    outputStream.WriteLine(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + " ==END");
                }
            }
        }