/// <summary> /// Copies the current contents of the BufferedTextWriter to another TextWriter /// </summary> /// <param name="Other">The TextWriter to copy the buffered text to</param> public void CopyTo(LineBasedTextWriter Other, string Prefix) { for (int Idx = 0; Idx < Lines.Count; Idx++) { Other.WriteLine("{0}{1}", Prefix, Lines[Idx]); } }
/// <summary> /// Overridden from ManagedTask. Executes the given action. /// </summary> /// <param name="Event">Event to set when the action completes</param> /// <param name="Writer">Writer for log output</param> protected override void InternalStart(ManualResetEvent Event, LineBasedTextWriter Writer) { DataReceivedEventHandler OutputHandler = (Sender, Args) => { if (!String.IsNullOrEmpty(Args.Data)) { Writer.WriteLine(Args.Data); } }; InnerProcess = new Process(); InnerProcess.StartInfo.FileName = FileName; InnerProcess.StartInfo.Arguments = Arguments; InnerProcess.StartInfo.UseShellExecute = false; InnerProcess.StartInfo.RedirectStandardOutput = true; InnerProcess.StartInfo.RedirectStandardError = true; InnerProcess.OutputDataReceived += OutputHandler; InnerProcess.ErrorDataReceived += OutputHandler; InnerProcess.Start(); InnerProcess.BeginOutputReadLine(); InnerProcess.BeginErrorReadLine(); if (Environment.GetEnvironmentVariable("XoreaxBuildContext") != "1") { try { InnerProcess.PriorityClass = ProcessPriorityClass.BelowNormal; } catch (Exception) { } } Event.SafeWaitHandle = new SafeWaitHandle(InnerProcess.Handle, false); }
/// <summary> /// Execute an external process, writing its output to the log. /// </summary> /// <param name="Executable">Path to the executable to run</param> /// <param name="Arguments">Arguments for the program</param> /// <param name="WorkingDir">The working directory to run in</param> /// <param name="Log">Writer to receive output messages</param> public static int Run(FileReference ExecutableFile, string Arguments, DirectoryReference WorkingDir, LineBasedTextWriter Log) { using (Process NewProcess = new Process()) { DataReceivedEventHandler EventHandler = (Sender, Args) => { if (!String.IsNullOrEmpty(Args.Data)) { Log.WriteLine(Args.Data); } }; NewProcess.StartInfo.FileName = ExecutableFile.FullName; NewProcess.StartInfo.Arguments = Arguments; NewProcess.StartInfo.UseShellExecute = false; NewProcess.StartInfo.WorkingDirectory = WorkingDir.FullName; NewProcess.StartInfo.RedirectStandardOutput = true; NewProcess.StartInfo.RedirectStandardError = true; NewProcess.OutputDataReceived += EventHandler; NewProcess.ErrorDataReceived += EventHandler; NewProcess.Start(); NewProcess.BeginOutputReadLine(); NewProcess.BeginErrorReadLine(); NewProcess.WaitForExit(); return(NewProcess.ExitCode); } }
/// <summary> /// Execute a Parallel.For loop, but output status messages showing progress every 5 seconds /// </summary> /// <param name="Message">The message to output</param> /// <param name="BeginValue">The lower bound for the for loop, inclusive</param> /// <param name="EndValue">The upper bound for the for loop, exclusive</param> /// <param name="Action">The action to execute for each iteration</param> /// <param name="Log">Log for output messages</param> public static void ParallelForWithStatus(string Message, int BeginValue, int EndValue, ParallelOptions Options, Action <int> Action, LineBasedTextWriter Log) { Log.WriteLine(Message); ParallelForState State = new ParallelForState(); Parallel.For(BeginValue, EndValue, Options, Index => { Action(Index); State.Increment(EndValue - BeginValue, Message, Log); }); State.Complete(EndValue - BeginValue, Message, Log); }
/// <summary> /// Increment the number of iterations, and output a status update message if 5 seconds have elapsed /// </summary> /// <param name="TotalCount">The total number of iterations</param> /// <param name="Message">The message to output</param> /// <param name="Log">Writer for output messages</param> public void Increment(int TotalCount, string Message, LineBasedTextWriter Log) { lock (this) { CompletedCount++; if (Timer.Elapsed > NextUpdateTime && CompletedCount < TotalCount) { lock (Log) { Log.WriteLine("[{0}] {1} ({2}/{3}; {4}%)", Timer.Elapsed.ToString(@"hh\:mm\:ss"), Message, CompletedCount, TotalCount, (CompletedCount * 100) / TotalCount); } NextUpdateTime = Timer.Elapsed + TimeSpan.FromSeconds(5.0); } } }
/// <summary> /// Print the last message showing 100% /// </summary> /// <param name="TotalCount">The total number of iterations</param> /// <param name="Message">The message to output</param> /// <param name="Log">Writer for output messages</param> public void Complete(int TotalCount, string Message, LineBasedTextWriter Log) { Log.WriteLine("[{0}] {1} ({2}/{3}; {4}%)", Timer.Elapsed.ToString(@"hh\:mm\:ss"), Message, TotalCount, TotalCount, 100); }