mirror of
https://github.com/SabreTools/MPF.git
synced 2026-07-02 17:24:48 +00:00
Add queueing mechanism to DumpEnvironment
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
@@ -70,12 +71,36 @@ namespace MPF.Utilities
|
||||
/// <param name="message">String value to report</param>
|
||||
public EventHandler<string> ReportStatus;
|
||||
|
||||
/// <summary>
|
||||
/// Queue of items that need to be logged
|
||||
/// </summary>
|
||||
private readonly ConcurrentQueue<string> outputQueue = new ConcurrentQueue<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Event handler for data returned from a process
|
||||
/// </summary>
|
||||
private void OutputToLog(object proc, string args)
|
||||
{
|
||||
ReportStatus.Invoke(this, args);
|
||||
outputQueue.Enqueue(args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Process the outputs in the queue
|
||||
/// </summary>
|
||||
private void ProcessOutputs()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// Nothing in the queue means we get to idle
|
||||
if (outputQueue.Count == 0)
|
||||
continue;
|
||||
|
||||
// Get the next item from the queue
|
||||
if (!outputQueue.TryDequeue(out string nextOutput))
|
||||
continue;
|
||||
|
||||
ReportStatus.Invoke(this, nextOutput);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -368,10 +393,16 @@ namespace MPF.Utilities
|
||||
if (!result)
|
||||
return result;
|
||||
|
||||
// Invoke output processing, if needed
|
||||
if (!Options.ToolsInSeparateWindow)
|
||||
{
|
||||
Task.Run(() => ProcessOutputs());
|
||||
Parameters.ReportStatus += OutputToLog;
|
||||
}
|
||||
|
||||
// Execute internal tool
|
||||
progress?.Report(Result.Success($"Executing {Options.InternalProgram}... {(Options.ToolsInSeparateWindow ? "please wait!" : "see log for output!")}"));
|
||||
Directory.CreateDirectory(OutputDirectory);
|
||||
Parameters.ReportStatus += OutputToLog;
|
||||
await Task.Run(() => Parameters.ExecuteInternalProgram(Options.ToolsInSeparateWindow));
|
||||
progress?.Report(Result.Success($"{Options.InternalProgram} has finished!"));
|
||||
|
||||
@@ -380,6 +411,10 @@ namespace MPF.Utilities
|
||||
result = await Task.Run(() => ExecuteAdditionalTools());
|
||||
progress?.Report(result);
|
||||
|
||||
// Remove evet habdler if needed
|
||||
if (!Options.ToolsInSeparateWindow)
|
||||
Parameters.ReportStatus -= OutputToLog;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user