#=================================================
[code:1][/code:1]
#Si powershell n'est pas en mode sta alors on relance le script en mode sta
if($host.Runspace.ApartmentState -ne 'STA')
{
PowerShell -Sta -File ($myInvocation.MyCommand.Path)
return
}
Add-Type –assemblyName PresentationFramework
Add-Type –assemblyName PresentationCore
Add-Type –assemblyName WindowsBase
#Declaration du code XAML
[xml]$xaml = @’
’@
#Declaration du code XAML
[xml]$xaml2 = @’
’@
#====================================================
#Code powershell
function test
{
$a = Start-Thread -scriptBlock { get-childitem c:\ * -recurse |export-csv c:\toto.csv
$i = 0
$progressBar1.value = 0
$progressBar1.Maximum = 100
ForEach ($Object in $a) {
$progressBar1.Value = $i++ / $a.count * 100
$Form.Dispatcher.Invoke( "Render", [Windows.Input.InputEventHandler]{ $progressBar1.UpdateLayout() }, $null, $null)
}
}}
$progressBar1 = $form.FindName('progressBar1’)
#====================================================
function New-Thread
{
$config = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace.RunspaceConfiguration
$runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($config)
$thread = New-Object System.Object
$thread = $thread | Add-Member NoteProperty "Runspace" $runspace -passThru
$thread = $thread | Add-Member NoteProperty "Pipeline" $null -passThru
$thread = $thread | Add-Member ScriptProperty "Running" { return ($this.Pipeline -ne $null -and (-not $this.Pipeline.Error.EndOfPipeline -or -not $this.Pipeline.Output.EndOfPipeline)) } -passThru
$thread.Runspace.Open()
return $thread
}
#=====================================================================================
function Start-Thread
{
param
(
[object] $thread = $null,
[ScriptBlock] $scriptBlock = $(throw "The parameter -scriptBlock is required.")
)
if ($thread -eq $null)
{
$thread = New-Thread
}
if ($thread.Running)
{
throw "The thread is already running, please wait for it complete before trying again."
}
$thread.Pipeline = $thread.Runspace.CreatePipeline($scriptBlock)
$thread.Pipeline.Input.Close()
$thread.Pipeline.InvokeAsync()
return $thread
}
#==========================================================================================
$reader=New-Object System.Xml.XmlNodeReader $xaml
$Form=[Windows.Markup.XamlReader]::Load($reader)
$button1 = $form.FindName(’button1’)
$button1.add_click( {
$form2=[Windows.Markup.XamlReader]::Load([IO.File]::OpenText('F:\t\ddd\test.xaml').basestream)
$btnBack = $form2.FindName('btnBack')
#$btnBack.add_click.Invoke({$form2.close()})
$form.close()
$button2 = $form2.FindName(’button2’)
$progressBar1 = $form2.FindName('progressBar1’)
$button2.add_click({test})
$form2.ShowDialog()
})
$Form.ShowDialog() | Out-Null:whistle: