Created
January 16, 2019 09:53
-
-
Save nohwnd/d1bed9a5b5efc78451ea6101d7567986 to your computer and use it in GitHub Desktop.
Refreshing browser from other runspace
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
$syncHash = [hashtable]::Synchronized(@{ | |
Browser = $null | |
Window = $null | |
}) | |
$psCmd = [powershell]::Create() | |
$newRunspace = [RunspaceFactory]::CreateRunspace() | |
$newRunspace.Open() | |
$newRunspace.SessionStateProxy.SetVariable('syncHash', $syncHash) | |
$psCmd.Runspace = $newRunspace | |
$sb = { | |
$c= 0 | |
while ($true) { | |
Start-Sleep -Seconds 10 | |
$c++ | |
if ($c % 2) { | |
$syncHash.Window.Dispatcher.invoke([action] { | |
try { | |
$syncHash.Window.Title = "google" | |
$syncHash.Browser.Navigate('http://www.google.com') | |
$syncHash.Window.InvalidateVisual() | |
} catch {} | |
}) | |
} | |
else { | |
$syncHash.Window.Dispatcher.invoke([action] { | |
try { | |
$syncHash.Window.Title = "bing" | |
$syncHash.Browser.Navigate('http://www.bing.com') | |
$syncHash.Window.InvalidateVisual() | |
} catch {} | |
}) | |
} | |
} | |
} | |
$psCmd.AddScript($sb) | |
$psCmd.BeginInvoke() | |
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') | |
[xml]$xaml = @' | |
<Window | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
Title="PowerShell HTML GUI" WindowStartupLocation="Manual"> | |
<Grid> | |
<WebBrowser | |
HorizontalAlignment="Left" | |
Width="415" | |
Height="340" | |
Margin="10,10,0,0" | |
VerticalAlignment="Top" | |
Name="WebBrowser" | |
/> | |
</Grid> | |
</Window> | |
'@ | |
$reader = New-Object System.Xml.XmlNodeReader($xaml) | |
$Form = [Windows.Markup.XamlReader]::Load($reader) | |
$Form.Width = 415 | |
$Form.Height = 340 | |
$Form.Topmost = $True | |
$WebBrowser = $Form.FindName('WebBrowser') | |
$WebBrowser.add_Loaded({$this.Navigate('http://www.bing.com')}) | |
$syncHash.Window = $Form | |
$syncHash.Browser = $WebBrowser | |
$Form.ShowDialog() | |
$psCmd.Stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment