Ejemplo n.º 1
0
 private void CloseSafely(StreamPipe pipe)
 {
     try
     {
         pipe.Close();
     }
     catch { }
 }
Ejemplo n.º 2
0
        protected override void OnStop()
        {
            try
            {
                string workingDirectory = WorkingDirectory;
                String shutdownScript   = GetStringSetting("ShutdownScript");

                if (!string.IsNullOrEmpty(shutdownScript))
                {
                    String shutdownScriptArguments = GetStringSetting("ShutdownScript.Arguments");
                    try
                    {
                        if (!File.Exists(shutdownScript))
                        {
                            if (!File.Exists(workingDirectory + "/" + shutdownScript))
                            {
                                throw new Exception(shutdownScript + " does not exist!");
                            }
                            shutdownScript = workingDirectory + "/" + shutdownScript;
                        }

                        Process process = new Process();
                        // Redirect the output stream of the child process.
                        process.StartInfo.UseShellExecute        = false;
                        process.StartInfo.RedirectStandardOutput = true;
                        process.StartInfo.RedirectStandardError  = true;
                        process.StartInfo.FileName  = shutdownScript;
                        process.StartInfo.Arguments = shutdownScriptArguments;

                        if (null != workingDirectory)
                        {
                            process.StartInfo.WorkingDirectory = workingDirectory;
                            Log(EventLogEntryType.Information, "Executing " + shutdownScript + " in working directory " + workingDirectory);
                        }
                        else
                        {
                            Log(EventLogEntryType.Information, "Executing " + shutdownScript);
                        }

                        process.Start();

                        PipeOutput(runningProcess);

                        process.WaitForExit();

                        int afterExitDelay = AfterExitDelay;
                        if (afterExitDelay > 0)
                        {
                            int delay = AfterExitDelay * 1000;
                            while (delay > 0 && null != runningProcess && !runningProcess.HasExited)
                            {
                                delay -= 10;
                                Thread.Sleep(10);
                            }
                        }
                    }
                    catch (Exception e) {
                        Log(EventLogEntryType.Error, e.Message);
                    }
                }
                else
                {
                    Log(EventLogEntryType.Information, "No shutdown script defined! Killing process!");
                }


                if (null != runningProcess && !runningProcess.HasExited)
                {
                    ProcessUtility.KillTree(runningProcess.Id);
                }
            }
            catch (Exception e)
            {
                Log(EventLogEntryType.Error, e.Message);
                throw (e);
            }
            finally
            {
                CloseSafely(outPipe);
                CloseSafely(errPipe);

                outPipe   = null;
                errPipe   = null;
                outStream = null;
                errStream = null;
            }
        }
Ejemplo n.º 3
0
 private void PipeOutput(Process process)
 {
     outPipe = new StreamPipe(process.StandardOutput.BaseStream, outStream.BaseStream);
     errPipe = new StreamPipe(process.StandardError.BaseStream, errStream.BaseStream);
 }