-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPowerShellPoolStreams.cs
More file actions
76 lines (64 loc) · 2.1 KB
/
Copy pathPowerShellPoolStreams.cs
File metadata and controls
76 lines (64 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;
namespace PSParallel
{
class PowerShellPoolStreams : IDisposable
{
public PSDataCollection<PSObject> Output { get; } = new PSDataCollection<PSObject>(100);
public PSDataCollection<DebugRecord> Debug { get; } = new PSDataCollection<DebugRecord>();
private PSDataCollection<ProgressRecord> Progress { get; } = new PSDataCollection<ProgressRecord>();
public PSDataCollection<ErrorRecord> Error { get; } = new PSDataCollection<ErrorRecord>();
public PSDataCollection<WarningRecord> Warning { get; } = new PSDataCollection<WarningRecord>();
public PSDataCollection<InformationRecord> Information { get; } = new PSDataCollection<InformationRecord>();
public PSDataCollection<VerboseRecord> Verbose { get; } = new PSDataCollection<VerboseRecord>();
public void Dispose()
{
Output.Dispose();
Debug.Dispose();
Progress.Dispose();
Error.Dispose();
Information.Dispose();
Verbose.Dispose();
}
public void AddProgress(ProgressRecord progress, int index)
{
DoAddProgress(progress);
OnProgressChanged(progress.PercentComplete, index);
}
public void ClearProgress(int index)
{
OnProgressChanged(0, index);
}
protected void DoAddProgress(ProgressRecord progress)
{
Progress.Add(progress);
}
protected virtual void OnProgressChanged(int progress, int index){}
public Collection<ProgressRecord> ReadAllProgress()
{
return Progress.ReadAll();
}
}
class ProgressTrackingPowerShellPoolStreams : PowerShellPoolStreams
{
private readonly int _maxPoolSize;
private readonly int[] _poolProgress;
private int _currentProgress;
public ProgressTrackingPowerShellPoolStreams(int maxPoolSize)
{
_maxPoolSize = maxPoolSize;
_poolProgress = new int[maxPoolSize];
}
protected override void OnProgressChanged(int progress, int index)
{
lock(_poolProgress) {
_poolProgress[index] = progress;
_currentProgress = _poolProgress.Sum();
}
}
public int PoolPercentComplete => _currentProgress/_maxPoolSize;
}
}