Skip to content

Instantly share code, notes, and snippets.

@proxb
Created February 12, 2015 02:58
Show Gist options
  • Save proxb/2757cc0c0684d53f4ec6 to your computer and use it in GitHub Desktop.
Save proxb/2757cc0c0684d53f4ec6 to your computer and use it in GitHub Desktop.
Example of not using synchronized hashtable vs. Script/Global scope for variables in forms
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$hash = [hashtable]::Synchronized(@{})
$hash.text = ""
$window = New-Object System.Windows.Forms.Form
$window.Width = 1000
$window.Height = 1000
$windowTextBox = New-Object System.Windows.Forms.TextBox
$windowTextBox.Location = New-Object System.Drawing.Size(10,10)
$windowTextBox.Size = New-Object System.Drawing.Size(500,500)
$windowButtonOK = New-Object System.Windows.Forms.Button
$windowButtonOK.Location = New-Object System.Drawing.Size(10,510)
$windowButtonOK.Size = New-Object System.Drawing.Size(50,50)
$windowButtonOK.Text = "OK"
$windowButtonOK.Add_Click({
$hash.text = $windowTextBox.Text
$window.Dispose()
})
$window.Controls.Add($windowTextBox)
$window.Controls.Add($windowButtonOK)
$result = $window.ShowDialog()
Write-Host $result
Write-Host -ForegroundColor Yellow $hash.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment