Question
Statut d'un Runspace
- Laurent
- Auteur du sujet
- Hors Ligne
- Membre senior
-
Réduire
Plus d'informations
- Messages : 71
- Remerciements reçus 0
il y a 8 ans 7 mois #24370
par Laurent
Statut d'un Runspace a été créé par Laurent
Bonjour à tous,
J'ai créer une interface graphique en WPF et Powershell, pour que la Gui ne reste pas figée pendant mes différentes opérations j'ai créer plusieurs runspace qui fonctionnent correctement.
Le soucis c'est que je souhaite que le runscpace1 (Installation 1) soit terminé pour lancer le runscpace2 (installation 2).
Car actuellement les deux runspace se lance en même temps et le second ne s'installe pas car un autre logiciel est en cours d'installation(chose logique).
Pour ce faire je souhaite récupérer le statut du runscpace1
(Competed, Running, NotStarted) afin de lancer le second runspace uniquement quand le premier sera terminé.
En langage francais sa donnerai
si la case N°2 est coché et que le runscpace1 est terminé (completed)alors on lance le runspace2
Donc comment récupérer le statut de mon Runspace 1 (Competed, Running, NotStarted) ?
J'espère avoir des réponse merci à tous.
[code:1]#################################################################
# Chargement des Librairies
#################################################################
[Reflection.Assembly]::LoadWithPartialName(\"PresentationFramework\"«»)
[Reflection.Assembly]::LoadWithPartialName(\"PresentationCore\"«»)
[Reflection.Assembly]::LoadWithPartialName(\"WindowsBase\"«»)
$syncHash = [hashtable]::«»Synchronized(@{})
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = \"STA\"
$Runspace.ThreadOptions = \"ReuseThread\"
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable(\"syncHash\",$syncHash)
$code = {
$Runspace1 =[runspacefactory]::CreateRunspace()
$Runspace1.ApartmentState = \"MTA\"
$Runspace1.ThreadOptions = \"ReuseThread\"
$Runspace1.Open()
$Runspace1.SessionStateProxy.SetVariable(\"syncHash\",$syncHash)
$Runspace2 =[runspacefactory]::CreateRunspace()
$Runspace2.ApartmentState = \"MTA\"
$Runspace2.ThreadOptions = \"ReuseThread\"
$Runspace2.Open()
$Runspace2.SessionStateProxy.SetVariable(\"syncHash\",$syncHash)
[xml]$xaml = @\"
<Window
xmlns=\"schemas.microsoft.com/winfx/2006/xaml/presentation\"
xmlns:«»x=\"schemas.microsoft.com/winfx/2006/xaml\"
xmlns:d=\"schemas.microsoft.com/expression/blend/2008\"
xmlns:mc=\"schemas.openxmlformats.org/markup-compatibility/2006\"
xmlns:local=\"clr-namespace:WpfApp6\"
Title=\"MainWindow\" Height=\"600\" Width=\"800\" ResizeMode=\"NoResize\" WindowStartupLocation=\"CenterScreen\">
<Grid>
<Button x:Name=\"installer\" Content=\"Installer\" HorizontalAlignment=\"Left\" Height=\"29\" Margin=\"578,480,0,0\" VerticalAlignment=\"Top\" Width=\"112\" FontSize=\"16\" Foreground=\"#FF0A247E\" FontWeight=\"Bold\" FontFamily=\"Verdana\"/>
<Button x:Name=\"quitter\" Content=\"Quitter\" HorizontalAlignment=\"Left\" Height=\"29\" Margin=\"420,480,0,0\" VerticalAlignment=\"Top\" Width=\"112\" FontSize=\"16\" Foreground=\"#FF0A247E\" FontWeight=\"Bold\" RenderTransformOrigin=\"-0.411,0.466\" FontFamily=\"Verdana\"/>
<CheckBox x:Name=\"check1\" Content=\"case pour installation 1\" HorizontalAlignment=\"Left\" Height=\"25\" Margin=\"140,150,0,0\" VerticalAlignment=\"Top\" Width=\"160\" RenderTransformOrigin=\"0.5,0.5\" VerticalContentAlignment=\"Center\" FontFamily=\"Verdana\" IsChecked=\"True\">
<CheckBox.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX=\"1.2\" ScaleY=\"1.2\"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</CheckBox.RenderTransform>
</CheckBox>
<CheckBox x:Name=\"check2\" Content=\"case pour installation 2\" HorizontalAlignment=\"Left\" Height=\"25\" Margin=\"140,190,0,0\" VerticalAlignment=\"Top\" Width=\"160\" RenderTransformOrigin=\"0.5,0.5\" VerticalContentAlignment=\"Center\" FontFamily=\"Verdana\" IsChecked=\"True\">
<CheckBox.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX=\"1.2\" ScaleY=\"1.2\"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</CheckBox.RenderTransform>
</CheckBox>
</Grid>
</Window>
\"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
$global:«»syncHash.check1 = $global:«»syncHash.Window.FindName(\"check1\"«»)
$global:«»syncHash.check2 = $global:«»syncHash.Window.FindName(\"check2\"«»)
$global:«»syncHash.boutoninstaller = $global:«»syncHash.Window.FindName(\"installer\"«»)
$global:«»syncHash.boutonquitter = $global:«»syncHash.Window.Findname(\"quitter\"«»)
$Installation1 = [PowerShell]::Create().AddScript({
Write-Host \"installation 1\"
})
$Installation2 = [PowerShell]::Create().AddScript({
Write-Host \"installation 2\"
})
$syncHash.boutoninstaller.Add_Click({
if($global:«»syncHash.check1.isChecked)
{$Installation1.Runspace = $Runspace1
$Installation1.BeginInvoke() | Out-Null}
if($global:«»syncHash.check2.isChecked)
{$Installation2.Runspace = $Runspace2
$Installation2.BeginInvoke() | Out-Null}
})
$global:«»syncHash.Window.ShowDialog()
$Runspace.Close()
$Runspace.Dispose()
}
$PSinstance1 = [powershell]::Create().AddScript($Code)
$PSinstance1.Runspace = $Runspace
$Job = $PSinstance1.BeginInvoke()
[/code:1]
J'ai créer une interface graphique en WPF et Powershell, pour que la Gui ne reste pas figée pendant mes différentes opérations j'ai créer plusieurs runspace qui fonctionnent correctement.
Le soucis c'est que je souhaite que le runscpace1 (Installation 1) soit terminé pour lancer le runscpace2 (installation 2).
Car actuellement les deux runspace se lance en même temps et le second ne s'installe pas car un autre logiciel est en cours d'installation(chose logique).
Pour ce faire je souhaite récupérer le statut du runscpace1
(Competed, Running, NotStarted) afin de lancer le second runspace uniquement quand le premier sera terminé.
En langage francais sa donnerai
si la case N°2 est coché et que le runscpace1 est terminé (completed)alors on lance le runspace2
Donc comment récupérer le statut de mon Runspace 1 (Competed, Running, NotStarted) ?
J'espère avoir des réponse merci à tous.
[code:1]#################################################################
# Chargement des Librairies
#################################################################
[Reflection.Assembly]::LoadWithPartialName(\"PresentationFramework\"«»)
[Reflection.Assembly]::LoadWithPartialName(\"PresentationCore\"«»)
[Reflection.Assembly]::LoadWithPartialName(\"WindowsBase\"«»)
$syncHash = [hashtable]::«»Synchronized(@{})
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = \"STA\"
$Runspace.ThreadOptions = \"ReuseThread\"
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable(\"syncHash\",$syncHash)
$code = {
$Runspace1 =[runspacefactory]::CreateRunspace()
$Runspace1.ApartmentState = \"MTA\"
$Runspace1.ThreadOptions = \"ReuseThread\"
$Runspace1.Open()
$Runspace1.SessionStateProxy.SetVariable(\"syncHash\",$syncHash)
$Runspace2 =[runspacefactory]::CreateRunspace()
$Runspace2.ApartmentState = \"MTA\"
$Runspace2.ThreadOptions = \"ReuseThread\"
$Runspace2.Open()
$Runspace2.SessionStateProxy.SetVariable(\"syncHash\",$syncHash)
[xml]$xaml = @\"
<Window
xmlns=\"schemas.microsoft.com/winfx/2006/xaml/presentation\"
xmlns:«»x=\"schemas.microsoft.com/winfx/2006/xaml\"
xmlns:d=\"schemas.microsoft.com/expression/blend/2008\"
xmlns:mc=\"schemas.openxmlformats.org/markup-compatibility/2006\"
xmlns:local=\"clr-namespace:WpfApp6\"
Title=\"MainWindow\" Height=\"600\" Width=\"800\" ResizeMode=\"NoResize\" WindowStartupLocation=\"CenterScreen\">
<Grid>
<Button x:Name=\"installer\" Content=\"Installer\" HorizontalAlignment=\"Left\" Height=\"29\" Margin=\"578,480,0,0\" VerticalAlignment=\"Top\" Width=\"112\" FontSize=\"16\" Foreground=\"#FF0A247E\" FontWeight=\"Bold\" FontFamily=\"Verdana\"/>
<Button x:Name=\"quitter\" Content=\"Quitter\" HorizontalAlignment=\"Left\" Height=\"29\" Margin=\"420,480,0,0\" VerticalAlignment=\"Top\" Width=\"112\" FontSize=\"16\" Foreground=\"#FF0A247E\" FontWeight=\"Bold\" RenderTransformOrigin=\"-0.411,0.466\" FontFamily=\"Verdana\"/>
<CheckBox x:Name=\"check1\" Content=\"case pour installation 1\" HorizontalAlignment=\"Left\" Height=\"25\" Margin=\"140,150,0,0\" VerticalAlignment=\"Top\" Width=\"160\" RenderTransformOrigin=\"0.5,0.5\" VerticalContentAlignment=\"Center\" FontFamily=\"Verdana\" IsChecked=\"True\">
<CheckBox.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX=\"1.2\" ScaleY=\"1.2\"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</CheckBox.RenderTransform>
</CheckBox>
<CheckBox x:Name=\"check2\" Content=\"case pour installation 2\" HorizontalAlignment=\"Left\" Height=\"25\" Margin=\"140,190,0,0\" VerticalAlignment=\"Top\" Width=\"160\" RenderTransformOrigin=\"0.5,0.5\" VerticalContentAlignment=\"Center\" FontFamily=\"Verdana\" IsChecked=\"True\">
<CheckBox.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX=\"1.2\" ScaleY=\"1.2\"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</CheckBox.RenderTransform>
</CheckBox>
</Grid>
</Window>
\"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
$global:«»syncHash.check1 = $global:«»syncHash.Window.FindName(\"check1\"«»)
$global:«»syncHash.check2 = $global:«»syncHash.Window.FindName(\"check2\"«»)
$global:«»syncHash.boutoninstaller = $global:«»syncHash.Window.FindName(\"installer\"«»)
$global:«»syncHash.boutonquitter = $global:«»syncHash.Window.Findname(\"quitter\"«»)
$Installation1 = [PowerShell]::Create().AddScript({
Write-Host \"installation 1\"
})
$Installation2 = [PowerShell]::Create().AddScript({
Write-Host \"installation 2\"
})
$syncHash.boutoninstaller.Add_Click({
if($global:«»syncHash.check1.isChecked)
{$Installation1.Runspace = $Runspace1
$Installation1.BeginInvoke() | Out-Null}
if($global:«»syncHash.check2.isChecked)
{$Installation2.Runspace = $Runspace2
$Installation2.BeginInvoke() | Out-Null}
})
$global:«»syncHash.Window.ShowDialog()
$Runspace.Close()
$Runspace.Dispose()
}
$PSinstance1 = [powershell]::Create().AddScript($Code)
$PSinstance1.Runspace = $Runspace
$Job = $PSinstance1.BeginInvoke()
[/code:1]
Connexion ou Créer un compte pour participer à la conversation.
- xyz
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 6311
- Remerciements reçus 69
il y a 8 ans 7 mois #24371
par xyz
Tutoriels PowerShell
Réponse de xyz sur le sujet Re:Statut d'un Runspace
Salut,
le runspace propose un event pour cela :
[code:1]
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace|gm -MemberType event
# TypeName: System.Management.Automation.Runspaces.LocalRunspace
#
# Name MemberType Definition
# ----
# AvailabilityChanged Event System.EventHandler`1[System.Management.Automation.Runspaces.RunspaceAvailabilityEven...
# StateChanged Event System.EventHandler`1[System.Management.Automation.Runspaces.RunspaceStateEventArgs] .
[/code:1]
Maintenant le pb est d'utiliser cet event sous WPF en Powershell...
le runspace propose un event pour cela :
[code:1]
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace|gm -MemberType event
# TypeName: System.Management.Automation.Runspaces.LocalRunspace
#
# Name MemberType Definition
# ----
# AvailabilityChanged Event System.EventHandler`1[System.Management.Automation.Runspaces.RunspaceAvailabilityEven...
# StateChanged Event System.EventHandler`1[System.Management.Automation.Runspaces.RunspaceStateEventArgs] .
[/code:1]
Maintenant le pb est d'utiliser cet event sous WPF en Powershell...
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- Laurent
- Auteur du sujet
- Hors Ligne
- Membre senior
-
Réduire
Plus d'informations
- Messages : 71
- Remerciements reçus 0
il y a 8 ans 7 mois #24373
par Laurent
Réponse de Laurent sur le sujet Re:Statut d'un Runspace
Bonjour Laurent,
Merci pour ta réponse.
Dans un ancien de tes post tu avais utilisé
[code:1]#[System.Management.Automation.Runspaces.PipelineState]
While ($RSDrag.LastPipelineState -eq \"Running\"«»)
{
Sleep 1
} [/code:1]
powershell-scripting.com/index.php?optio...id=5166&catid=14
Donc dans mon cas le résultat serait :
[code:1]Runspcace1.LastPipelineState -eq \"Completed\"[/code:1] ?
Merci pour ta réponse.
Dans un ancien de tes post tu avais utilisé
[code:1]#[System.Management.Automation.Runspaces.PipelineState]
While ($RSDrag.LastPipelineState -eq \"Running\"«»)
{
Sleep 1
} [/code:1]
powershell-scripting.com/index.php?optio...id=5166&catid=14
Donc dans mon cas le résultat serait :
[code:1]Runspcace1.LastPipelineState -eq \"Completed\"[/code:1] ?
Connexion ou Créer un compte pour participer à la conversation.
- xyz
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 6311
- Remerciements reçus 69
il y a 8 ans 7 mois #24377
par xyz
Tutoriels PowerShell
Réponse de xyz sur le sujet Re:Statut d'un Runspace
Dans ce cas tu fais une boucle while d'attente de changement de status. A tester dans un GUI
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- Laurent
- Auteur du sujet
- Hors Ligne
- Membre senior
-
Réduire
Plus d'informations
- Messages : 71
- Remerciements reçus 0
il y a 8 ans 7 mois #24378
par Laurent
Réponse de Laurent sur le sujet Re:Statut d'un Runspace
j'ai tenté un While, mais cela ne fonctionne pas une idée ?
[code:1]While (Runspcace1.LastPipelineState -eq \"Completed\"«»)
{$Installation2.Runspace = $Runspace2
$Installation2.BeginInvoke() | Out-Null}[/code:1]
[code:1]While (Runspcace1.LastPipelineState -eq \"Completed\"«»)
{$Installation2.Runspace = $Runspace2
$Installation2.BeginInvoke() | Out-Null}[/code:1]
Connexion ou Créer un compte pour participer à la conversation.
- xyz
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 6311
- Remerciements reçus 69
Temps de génération de la page : 0.047 secondes
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- Statut d'un Runspace