# WPF which event to choose ?

**URL:** <https://forums.powershell.org/t/wpf-which-event-to-choose/11706>\
**Category:** PowerShell Help\
**Created:** [November 22, 2018, 10:53am UTC](https://forums.powershell.org/t/wpf-which-event-to-choose/11706 "2018-11-22T10:53:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![striper](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@striper](https://forums.powershell.org/u/striper)\
**Post date:** [November 22, 2018, 10:53am UTC](https://forums.powershell.org/t/wpf-which-event-to-choose/11706/1 "2018-11-22T10:53:26Z")

</div>

Hi everyone,

my goal is to start a process when the GUI is open and see the progress bar growing.

The process work fine, but I only see the render at the end of the process and not during the process.

I’ve tried to change the event (Activated/ContentRendered/Initialized/Loaded) but I still not have the correct result.

Or maybe there is another way to do it ?

[pre]

add-type -AssemblyName PresentationFramework

[xml]$FormoutFile = @"

\<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

Title="MainWindow" Height="450" Width="800"\>

\<Canvas HorizontalAlignment="Left" VerticalAlignment="Top" \>

\<ProgressBar Name="PB" Height="20" Canvas.Left="0" Canvas.Top="0" Width="792"/\>

\<TextBlock Name="TB\_Text" Canvas.Left="0" TextWrapping="Wrap" Canvas.Top="21" Height="370" Width="792" Text="TextBlock"/\>

\<Button Name="BT\_Close" Content="Close" Canvas.Left="0" Canvas.Top="391" Width="75" Height="20" /\>

\</Canvas\>

\</Window\>

"@

$NR = (New-Object System.Xml.XmlNodeReader $FormoutFile)

$Formout = [Windows.Markup.XamlReader]::load($NR)

$ProgessBar = $Formout.findName("PB")

$TextOut = $Formout.findName("TB\_Text")

$BT\_Close = $Formout.findName("BT\_Close")

$Formout.Add\_Loaded({

$BT\_Close.isEnabled=$false

$Progress=0

$ProgessBar.value=$Progress

$TextOut.text=@("START`n")

For ($i=0;$i-lt10;$i++){

start-sleep1

$TextOut.text+=@("Step $i`n")

$Progress+=$i\*10

$ProgessBar.value=$Progress

}

$TextOut.text+=@("END")

$BT\_Close.isEnabled=$true

})

$BT\_Close.Add\_Click({

$Formout.close()

})

$Formout.showdialog()

[/pre]

---

<div class="post-metadata">

**Author:** ![willprather](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@willprather](https://forums.powershell.org/u/willprather)\
**Post date:** [November 22, 2018, 12:42pm UTC](https://forums.powershell.org/t/wpf-which-event-to-choose/11706/2 "2018-11-22T12:42:23Z")

</div>

The reason for seeing the progress at the end is because the script you are showing is single threaded. Essentially, it can’t do the work and show the work at the same time.

I’ve spent a lot of time doing multi-threaded windows at this point, but I got it started from this blog series, [https://foxdeploy.com/2016/05/17/part-v-powershell-guis-responsive-apps-with-progress-bars/](https://foxdeploy.com/2016/05/17/part-v-powershell-guis-responsive-apps-with-progress-bars/).

Stephen Owen does a really good job of showing how this exact thing is done.

Happy Coding!

---

<div class="post-metadata">

**Author:** ![striper](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@striper](https://forums.powershell.org/u/striper)\
**Post date:** [November 23, 2018, 3:04am UTC](https://forums.powershell.org/t/wpf-which-event-to-choose/11706/3 "2018-11-23T03:04:50Z")

</div>

Many thanks,

I learn new stuff every days on Powershell, 5 days ago I don’t know WPF, now I’m going to do multi thread.

Going back to my code 😉

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/wpf-which-event-to-choose/11706/4 "2024-05-16T20:34:06Z")

</div>


