I know asking general questions in not the best thing to do, but I’m kind of lost and don’t know where to look further, so if you’ve seen my previous question I’ve managed to accomplish what I need with runspaces, but I, sadly, don’t really understand how it works ![]()
#region Initialize stuff
$files = gci "C:\logs\*.log"
$result = [System.Collections.ArrayList]::Synchronized((New-Object System.Collections.ArrayList))
$RunspaceCollection = @()
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1,5)
$RunspacePool.Open()
$ScriptBlock = {
Param($file, $result)
$content = Get-Content $file.FullName -ReadCount 0
foreach ($line in $content) {
if ($line -match 'A002') {
[void]$result.Add($($line -replace ' +',","))
}}}
#endregion
#region Process Data
Foreach ($file in $files) {
$Powershell = [PowerShell]::Create().AddScript($ScriptBlock).AddArgument($file).AddArgument($result)
$Powershell.RunspacePool = $RunspacePool
[Collections.Arraylist]$RunspaceCollection += New-Object -TypeName PSObject -Property @{
Runspace = $PowerShell.BeginInvoke()
PowerShell = $PowerShell
}}
While($RunspaceCollection) {
Foreach ($Runspace in $RunspaceCollection.ToArray()) {
If ($Runspace.Runspace.IsCompleted) {
[void]$result.Add($Runspace.PowerShell.EndInvoke($Runspace.Runspace))
$Runspace.PowerShell.Dispose()
$RunspaceCollection.Remove($Runspace)
}}}
#endregion
#region Parse Data
$data = ConvertFrom-Csv -InputObject $result -Header "1","2","3","TimeIn","TimeOut","4","5","Dur"
foreach ($line in $data) {
if ($line.TimeIn -match "A002") { $TimeIn += [timespan]::Parse($line.Dur) }
else { $TimeOut += [timespan]::Parse($line.Dur) }}
#endregion
So the thing I don’t really understand is why do I need this “[System.Collections.ArrayList]::Synchronized((New-Object System.Collections.ArrayList))”, why it doesn’t work with regular array list and what does synchronized mean?
Could you point me to some sources explaining this or explain this if it doesn’t require a lot of typing, please! Thank you!
ps. I obviously tried googling that, but failed.