Browse Source

GetDecompiledProjectCmdlet: remove unnecessary use of ConcurrentQueue

pull/1930/head
Siegfried Pammer 5 years ago
parent
commit
350c54d575
  1. 27
      ICSharpCode.Decompiler.PowerShell/GetDecompiledProjectCmdlet.cs

27
ICSharpCode.Decompiler.PowerShell/GetDecompiledProjectCmdlet.cs

@ -21,18 +21,19 @@ namespace ICSharpCode.Decompiler.PowerShell
[ValidateNotNullOrEmpty] [ValidateNotNullOrEmpty]
public string LiteralPath { get; set; } public string LiteralPath { get; set; }
readonly object syncObject = new object();
int completed; int completed;
string fileName; string fileName;
ConcurrentQueue<ProgressRecord> progress = new ConcurrentQueue<ProgressRecord>(); ProgressRecord progress;
public void Report(DecompilationProgress value) public void Report(DecompilationProgress value)
{ {
int current = completed; lock (syncObject) {
int next = current + 1; completed++;
next = Interlocked.CompareExchange(ref completed, next, current); progress = new ProgressRecord(1, "Decompiling " + fileName, $"Completed {completed} of {value.TotalNumberOfFiles}: {value.Status}") {
progress.Enqueue(new ProgressRecord(1, "Decompiling " + fileName, $"Completed {next} of {value.TotalNumberOfFiles}: {value.Status}") { PercentComplete = (int)(completed * 100.0 / value.TotalNumberOfFiles)
PercentComplete = (int)(next * 100.0 / value.TotalNumberOfFiles) };
}); }
} }
protected override void ProcessRecord() protected override void ProcessRecord()
@ -51,12 +52,14 @@ namespace ICSharpCode.Decompiler.PowerShell
Thread.Sleep(timeout); Thread.Sleep(timeout);
while (!task.IsCompleted) { while (!task.IsCompleted) {
if (progress.TryDequeue(out var record)) { ProgressRecord progress;
while (progress.TryDequeue(out var next)) { lock (syncObject) {
record = next; progress = this.progress;
} this.progress = null;
}
if (progress != null) {
timeout = 100; timeout = 100;
WriteProgress(record); WriteProgress(progress);
} else { } else {
Thread.Sleep(timeout); Thread.Sleep(timeout);
timeout = Math.Min(1000, timeout * 2); timeout = Math.Min(1000, timeout * 2);

Loading…
Cancel
Save