Created
June 5, 2024 21:03
-
-
Save santisq/898f0041e0f1323817a30c9a1c6dca47 to your computer and use it in GitHub Desktop.
easiest example i could think of using a runspace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } |
Author
santisq
commented
Jun 5, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment