-
-
Save MVKozlov/a4057126fbb291478c5487c1de0b77bb to your computer and use it in GitHub Desktop.
This file contains 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
$RunSpace=[RunspaceFactory]::CreateRunspace() | |
$RunSpace.ApartmentState = "STA" | |
$RunSpace.Open() | |
$PowerShell = [PowerShell]::Create() | |
$PowerShell.Runspace = $RunSpace | |
[void]$PowerShell.AddScript({ | |
#region MainWindow Script | |
Add-Type -AssemblyName presentationframework, presentationcore | |
#region Build the GUI | |
$xaml = @" | |
<Window x:Class="TestWPF.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:local="clr-namespace:TestWPF" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="150" Width="500"> | |
<StackPanel> | |
<Label Name="Label1" HorizontalAlignment="Center">Message</Label> | |
<Button Name="Button1" Width="200" Height="25">Press me</Button> | |
<Label></Label> | |
<ProgressBar Name="Progress1" Width="400" Height="25" Value="00" Maximum="100" ></ProgressBar> | |
</StackPanel> | |
</Window> | |
"@ | |
$xaml = $xaml -replace 'x:Class=".*?"' -replace 'mc:Ignorable="d"' -replace 'xmlns:local=".*?"' | |
$xml=[xml]$xaml | |
$reader=(New-Object System.Xml.XmlNodeReader $xml) | |
#endregion | |
$syncHash = [hashtable]::Synchronized(@{}) | |
$syncHash.MyValue = 0 | |
$syncHash.LongJob = $null | |
#region Connect to Controls | |
$syncHash.Window = [Windows.Markup.XamlReader]::Load( $reader ) | |
$syncHash.Label = $syncHash.Window.FindName('Label1') | |
$syncHash.Button = $syncHash.Window.FindName('Button1') | |
$syncHash.Progress = $syncHash.Window.FindName('Progress1') | |
#endregion | |
#region LogJob | |
$LongJobScriptBlock = { | |
param($syncHash) | |
$exiting = $false | |
$syncHash.Label.Dispatcher.Invoke('Background', | |
[action]{ $syncHash.Label.Content = 'Do long Job' } | |
) | |
do { | |
$syncHash.MyValue++ | |
if ($syncHash.MyValue -gt 100) { | |
$exiting = $true | |
} | |
$syncHash.Progress.Dispatcher.Invoke('Background', | |
[action]{ $syncHash.Progress.Value = $syncHash.MyValue } | |
) | |
Start-Sleep -MilliSeconds 100 | |
} until ($exiting) | |
$syncHash.Label.Dispatcher.Invoke('Background', | |
[action]{ $syncHash.Label.Content = 'Job Done' } | |
) | |
$syncHash.Button.Dispatcher.Invoke('Background', | |
[action]{ $syncHash.Button.IsEnabled = $true } | |
) | |
} | |
#endregion | |
#region UI actions | |
$syncHash.Button.Add_Click({ | |
if ($SyncHash.LongJob -eq $null -or $SyncHash.LongJob.State -eq 'Completed') { | |
# New Job | |
$SyncHash.MyValue = 0 | |
$SyncHash.Progress.Value = 0 | |
$SyncHash.LongJob = Start-RSJob $LongJobScriptBlock -ArgumentList $SyncHash | |
$SyncHash.Button.IsEnabled = $false | |
} | |
}) | |
#endregion | |
[void]$SyncHash.Window.Dispatcher.InvokeAsync{$SyncHash.Window.ShowDialog()}.Wait() | |
#endregion | |
}).BeginInvoke() | |
#[void]$syncHash.Window.ShowDialog() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment