Skip to content

Instantly share code, notes, and snippets.

@santisq
Created June 5, 2024 21:03
Show Gist options
  • Save santisq/898f0041e0f1323817a30c9a1c6dca47 to your computer and use it in GitHub Desktop.
Save santisq/898f0041e0f1323817a30c9a1c6dca47 to your computer and use it in GitHub Desktop.
easiest example i could think of using a runspace
using namespace System.Management.Automation.Runspaces
using namespace System.Windows.Forms
using namespace System.Drawing
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
$form = [Form]@{ StartPosition = [FormStartPosition]::CenterScreen }
$iss = [initialsessionstate]::CreateDefault2()
$iss.Variables.Add([SessionStateVariableEntry]::new('form', $form, ''))
$rs = [runspacefactory]::CreateRunspace($iss)
$rs.Open()
$txtBox = [TextBox]@{
Location = '10, 10'
Size = '200, 200'
Font = [Font]::new('Segoe UI', 15)
ReadOnly = $true
Name = 'myTxtBox'
}
$btn = [Button]@{
Location = '10, 50'
Text = 'Click Me!'
Name = 'myBtn'
}
$btn.Add_Click({
$this.Enabled = $false
$ps = [powershell]::Create().AddScript({
$txtBox = $form.Controls.Find('myTxtBox', $false)[0]
$txtBox.Clear()
'hello world!'.GetEnumerator() | ForEach-Object {
$txtBox.Text += $_
Start-Sleep -Milliseconds 500
}
$form.Controls.Find('myBtn', $false)[0].Enabled = $true
})
$ps.Runspace = $rs
$ps.BeginInvoke()
})
$form.Controls.AddRange(@($txtBox, $btn))
$form.ShowDialog()
if ($rs) { $rs.Dispose() }
@santisq
Copy link
Author

santisq commented Jun 5, 2024

Code_uZRrfQlWJY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment