< Back
Lock-Object
Post
NAME Lock-Object
SYNOPSIS
Locks an object to prevent simultaneous access from another thread.
SYNTAX
Lock-Object [-InputObject] <Object> [-ScriptBlock] <ScriptBlock> [<CommonParameters>]
DESCRIPTION
PowerShell implementation of C#'s "lock" statement. Code executed in the script block does not have to worry
about simultaneous modification of the object by code in another thread.
PARAMETERS
-InputObject <Object>
The object which is to be locked. This does not necessarily need to be the actual object you want to access;
it's common for an object to expose a property which is used for this purpose, such as the
ICollection.SyncRoot property.
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ScriptBlock <ScriptBlock>
The script block that is to be executed while you have a lock on the object.
Note: This script block is "dot-sourced" to run in the same scope as the caller. This allows you to assign
variables inside the script block and have them be available to your script or function after the end of the
lock block, if desired.
Required? true
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
None. This command does not accept pipeline input.
OUTPUTS
System.Object (depends on what's in the script block.)
NOTES
Most of the time, PowerShell code runs in a single thread. You have to go through several steps to create a
situation in which multiple threads can try to access the same .NET object. In the Links section of this help
topic, there is a blog post by Boe Prox which demonstrates this.
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>$hashTable = @{}
lock $hashTable.SyncRoot {
$hashTable.Add("Key", "Value")
}
This is an example of using the "lock" alias to Lock-Object, in a manner that most closely resembles the similar
C# syntax with positional parameters.
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>$hashTable = @{}
Lock-Object -InputObject $hashTable.SyncRoot -ScriptBlock {
$hashTable.Add("Key", "Value")
}
This is the same as Example 1, but using the full PowerShell command and parameter names.
RELATED LINKS
http://learn-powershell.net/2013/04/19/ ... runspaces/
SYNOPSIS
Locks an object to prevent simultaneous access from another thread.
SYNTAX
Lock-Object [-InputObject] <Object> [-ScriptBlock] <ScriptBlock> [<CommonParameters>]
DESCRIPTION
PowerShell implementation of C#'s "lock" statement. Code executed in the script block does not have to worry
about simultaneous modification of the object by code in another thread.
PARAMETERS
-InputObject <Object>
The object which is to be locked. This does not necessarily need to be the actual object you want to access;
it's common for an object to expose a property which is used for this purpose, such as the
ICollection.SyncRoot property.
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ScriptBlock <ScriptBlock>
The script block that is to be executed while you have a lock on the object.
Note: This script block is "dot-sourced" to run in the same scope as the caller. This allows you to assign
variables inside the script block and have them be available to your script or function after the end of the
lock block, if desired.
Required? true
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
None. This command does not accept pipeline input.
OUTPUTS
System.Object (depends on what's in the script block.)
NOTES
Most of the time, PowerShell code runs in a single thread. You have to go through several steps to create a
situation in which multiple threads can try to access the same .NET object. In the Links section of this help
topic, there is a blog post by Boe Prox which demonstrates this.
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>$hashTable = @{}
lock $hashTable.SyncRoot {
$hashTable.Add("Key", "Value")
}
This is an example of using the "lock" alias to Lock-Object, in a manner that most closely resembles the similar
C# syntax with positional parameters.
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>$hashTable = @{}
Lock-Object -InputObject $hashTable.SyncRoot -ScriptBlock {
$hashTable.Add("Key", "Value")
}
This is the same as Example 1, but using the full PowerShell command and parameter names.
RELATED LINKS
http://learn-powershell.net/2013/04/19/ ... runspaces/