Runspaces question

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Richard Diphoorn Richard Diphoorn 8 months, 1 week ago.

  • Author
    Posts
  • #37236

    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.

  • #37255
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    It's not really my field, because the code you have is really develop'ish.

    Because PowerShell is built on top of .NET, you can access all .NET classes from within PowerShell. So a developer can more or less use the same techniques as he/she is used to in for example C#.

    In your example, the [System.Collections.ArrayList] object is a fancy Array object. In an ArrayList you can dynamically add elements, in which an Array you cannot. ( https://msdn.microsoft.com/en-us/library/41107z8a.aspx )

    The double colon you see is an operator. This is used to access the static member. With this you can access the methods and properties on a class.

    The synchronized method is being use to create a so-called wrapper around the arraylist, to make sure it's thread safe. ( https://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx#Anchor_9 )

    As I said, programming is not really my piece of cake, but I hope it will help you.

You must be logged in to reply to this topic.