{"id":6071,"date":"2008-05-27T05:12:00","date_gmt":"2008-05-27T05:12:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2008\/05\/27\/wpf-amp-powershell-part-6-running-functions-in-the-background\/"},"modified":"2019-02-18T13:15:52","modified_gmt":"2019-02-18T20:15:52","slug":"wpf-amp-powershell-part-6-running-functions-in-the-background","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/wpf-amp-powershell-part-6-running-functions-in-the-background\/","title":{"rendered":"WPF &#038;amp; PowerShell &#8211; Part 6 (Running Functions in the Background)"},"content":{"rendered":"<p>For the next part in the series, I&#8217;ve introduced another update to the Show-Control function you first met in Part 4.&nbsp; In this update, I&#8217;ve added the ability to run the control you&#8217;ve created in a background runspace as a switch.<\/p>\n<p>In order to do this, Show-Control employs a neat little trick.&nbsp; It actually creates a background runspace, uses Get-Command to find out its own definition, and packs itself into the runspace it just created.&nbsp; Then it removes the switch parameter that told it to run in the background, but adds every other argument.&nbsp; It also makes sure to recreate the event handlers that were passed as an argument, since the script block will be running in a different runspace.&nbsp; The changes also fire some powershell events and create a variable in the background runspace that stores the parent host, but we&#8217;ll get to why those two are there tomorrow.<\/p>\n<p>Just having -backgroundRunspace makes taking the video player and turning it into a more complete control a little simpler to do.&nbsp; You can use =backgroundRunspace on any of the existing examples that use XAML (controls will not cross runspaces nicely), but just having it around makes having a lightweight video player applet really easy.&nbsp; Here it is.&nbsp; Our video player is ~40 lines, has play, pause, and stop buttons, and a slider that will let us control the position within the clip.<\/p>\n<p>@&#8221; <br \/>&lt;StackPanel xmlns=&#8217;<a href=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation'\">http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation&#8217;<\/a>&gt; <br \/>&nbsp;&nbsp;&nbsp; &lt;Label FontSize=&#8217;14&#8217;&gt;PowerShell Video Player &#8211; Drag a file here to play it&lt;\/Label&gt; <br \/>&nbsp;&nbsp;&nbsp; &lt;MediaElement Name=&#8221;MediaPlayer&#8221; Height=&#8221;240&#8243; Width=&#8221;320&#8243; LoadedBehavior=&#8221;Manual&#8221; \/&gt;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp; &lt;Slider Name=&#8221;Position&#8221; Minimum=&#8221;0&#8243; Maximum=&#8221;100&#8243; \/&gt; <br \/>&nbsp;&nbsp;&nbsp; &lt;StackPanel Orientation=&#8221;Horizontal&#8221;&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Button Name=&#8221;PlayButton&#8221;&gt;Play&lt;\/Button&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Button Name=&#8221;PauseButton&#8221;&gt;Pause&lt;\/Button&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Button Name=&#8221;StopButton&#8221;&gt;Stop&lt;\/Button&gt; <br \/>&nbsp;&nbsp;&nbsp; &lt;\/StackPanel&gt; <br \/>&lt;\/StackPanel&gt; <br \/>&#8220;@ | Show-Control -windowProperties @{&#8220;AllowDrop&#8221;=$true} -backgroundRunspace -event @{ <br \/>&nbsp;&nbsp;&nbsp; &#8220;Window.Activated&#8221; = { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $script:mediaPlayer = $window.Content.FindName(&#8220;MediaPlayer&#8221;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp; &#8220;Window.Drop&#8221;={&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $mediaPlayer.Source = New-Object System.URI ($_.Data.GetFileDropList() | select -first 1) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $mediaPlayer.Play() <br \/>&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp; &#8220;Window.Closed&#8221;= { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $mediaPlayer.Stop() <br \/>&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp; &#8220;StopButton.Click&#8221;={ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $mediaPlayer.Stop() <br \/>&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp; &#8220;PauseButton.Click&#8221;={ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $mediaPlayer.Pause() <br \/>&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp; &#8220;PlayButton.Click&#8221;={ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $mediaPlayer.Play()&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp; &#8220;Position.ValueChanged&#8221;={ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $positionControl = $window.Content.FindName(&#8220;Position&#8221;) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($mediaPlayer.NaturalDuration.HasTimespan) { <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $percentPerSecond = $mediaPlayer.NaturalDuration.Timespan.TotalSeconds \/ 100 <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $position = $positionControl.Value <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $mediaPlayer.Position = New-Timespan -second ($position * $percentPerSecond) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $mediaPlayer.Play() <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br \/>&nbsp;&nbsp;&nbsp; } <br \/>}<\/p>\n<p>While a video player is a prime example of a type of application that you might want to run in the background, it&#8217;s only a one way street.&nbsp; On our final day of the series we&#8217;ll show you how to use our updated Show-Control to make controls in the background stream data and talk to the main runspace.<\/p>\n<p>Hope this Helps,<\/p>\n<p>James Brundage [MSFT]<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/08\/55\/43\/61\/Show-Control.ps1\">Show-Control.ps1<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For the next part in the series, I&#8217;ve introduced another update to the Show-Control function you first met in Part 4.&nbsp; In this update, I&#8217;ve added the ability to run the control you&#8217;ve created in a background runspace as a switch. In order to do this, Show-Control employs a neat little trick.&nbsp; It actually creates [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[313,360],"class_list":["post-6071","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-show-control","tag-wpf"],"acf":[],"blog_post_summary":"<p>For the next part in the series, I&#8217;ve introduced another update to the Show-Control function you first met in Part 4.&nbsp; In this update, I&#8217;ve added the ability to run the control you&#8217;ve created in a background runspace as a switch. In order to do this, Show-Control employs a neat little trick.&nbsp; It actually creates [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=6071"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6071\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=6071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=6071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=6071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}