-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathtestrunspaces
More file actions
82 lines (69 loc) · 3.27 KB
/
testrunspaces
File metadata and controls
82 lines (69 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<Window
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:WpfApp6"
Title="MainWindow" Height="261.787" Width="779.468" Topmost="True">
<Grid>
<ProgressBar Name="ProgressBar" HorizontalAlignment="Left" Height="121" VerticalAlignment="Top" Width="497" Margin="115,29,0,0" Background="{x:Null}">
<ProgressBar.OpacityMask>
<ImageBrush ImageSource="c:\temp\completed.png"/>
</ProgressBar.OpacityMask>
</ProgressBar>
<TextBlock Name="TextBox" HorizontalAlignment="Left" Margin="330,155,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
<Button Name="Button" Content="Button" HorizontalAlignment="Left" Margin="686,201,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
#Load required libraries
Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase, System.Windows.Forms, System.Drawing
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$code = {
#$Path = Join-Path $PSScriptRoot -ChildPath 'Temp'
$XamlPath = 'C:\Temp\Form.xaml'
[xml]$xaml = Get-Content $XamlPath
#Read the form
$syncHash = [hashtable]::Synchronized(@{})
$Reader = (New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Form = [Windows.Markup.XamlReader]::Load($reader)
#AutoFind all controls
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object {
#New-Variable -Name $syncHash.($_.Name) -Value $syncHash.Form.FindName($_.Name) -Force
$syncHash.Add($_.Name, $syncHash.Form.FindName($_.Name))
}
function ProgressBar {
param($syncHash,$Count = 100,$miliseconds = 100)
$syncHash.Host = $host
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$Runspace.SessionStateProxy.SetVariable("Count",$Count)
$Runspace.SessionStateProxy.SetVariable("miliseconds",$miliseconds)
$code = {
$syncHash.form.Dispatcher.invoke([action]{ $syncHash.ProgressBar.Value = 0})
for ($i=0; $i -lt $Count; $i++) {
$syncHash.form.Dispatcher.invoke([action]{$syncHash.ProgressBar.value = $i})
$syncHash.form.Dispatcher.invoke([action]{$syncHash.TextBox.Text = "$i%"})
Start-Sleep -Milliseconds $miliseconds
}
}
$PSinstance = [powershell]::Create().AddScript($Code)
$PSinstance.Runspace = $Runspace
$job = $PSinstance.BeginInvoke()
}
$syncHash.Button.Add_Click({
ProgressBar -Count 100 -Milliseconds 100
})
$syncHash.Form.ShowDialog() | Out-Null
$syncHash.Error = $Error
$Runspace.Close()
$Runspace.Dispose()
}
$PSinstance1 = [powershell]::Create().AddScript($Code)
$PSinstance1.Runspace = $Runspace
$job = $PSinstance1.BeginInvoke()