Important plantage form

Plus d'informations
il y a 10 ans 9 mois #19769 par gaetan
plantage form a été créé par gaetan
Bonjour

Je viens vers vous car j'ai fait un script en powershell avec une form

dans cette form , je choisi parmis une Liste de X VM le nombre de VM pour lequel je veut effectuer une action et je clique sur start

mon start redirige vers une fonction qui prends 30 min de traitement par VM

Le pb etant que ma form est figé pendant , le traitement , celui ci fonctionne , le foreach fonctionne , l'ensemble du traitement est ok , mais vu que ma form est figé , je peut pas envoyé dans mon interface graphique le % d'avancement

Avez vous une solution simple pour isolé la form dans une bulle à part ?

je vais bidouillé une form simple pour pouvoir jouer le code..

merci d'avance

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 10 ans 9 mois #19771 par gaetan
Réponse de gaetan sur le sujet Re:plantage form
Bonjour

Voila , j'ai fait un petit jeu de test pour pouvoir expliquer plus en detail mon pb , j'ai une form ci dessous qui recupere une Liste de pc , je choisi X pc dans cette listes et je click sur lancer le traitement

Dans ce traitement , j'ai un foreach pour passer dans une fonction pour chaque pc selectionné , puis un start sleep de 60 s simulant le fait que ma fonction va prendre 60 s à etre traité

Le script fonctionne tres bien , par contre pendant les 60 s de traitement la form est figé , avait vous une solution ?

Je pense qu'il faut passer par des threads mais je n'est pas reussi à l'appliquer

merci d'avance

cdt

[code:1]

cls

#region bouton_refresh
function bouton_refresh {

$tcintegration.Items.Add(\"PC 1 / NUM SERIE / IP / MAC\"«»)
$tcintegration.Items.Add(\"PC 2 / NUM SERIE / IP / MAC\"«»)
$tcintegration.Items.Add(\"PC 3 / NUM SERIE / IP / MAC\"«»)


$totalTCintegration.text = $tcintegration.items.Count
}
#endregion





#region action_si_click_sur_TCintegration
function action_si_click_sur_TCintegration {


if (!($listeTCasupprimer.items -match $TCintegration.selecteditem)){$listeTCasupprimer.Items.Add( $TCintegration.selecteditem )}

}
#endregion



#region action_si_click_sur_listedesTCasupprimer
function action_si_click_sur_listedesTCasupprimer {

$listeTCasupprimer.Items.remove( $listeTCasupprimer.selecteditem )

}
#endregion




#region bouton_lancer_la_suppression
function bouton_lancer_la_suppression {

foreach( $listeTCasupprimerunique in $listeTCasupprimer.items )
{
try
{
$suppression_en_cours.Text=\"\"
$suppression_en_cours.Text=\"$listeTCasupprimerunique traitement en cours ...\"

#traitement.
#traitement..
#traitement...

Start-Sleep 60



}
catch
{
continue
}

}




}
#endregion



#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.6.0
# Generated On: 18/05/2015 15:01
########################################################################

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname(\"System.Drawing\"«») | Out-Null
[reflection.assembly]::loadwithpartialname(\"System.Windows.Forms\"«») | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$label2 = New-Object System.Windows.Forms.Label
$suppression_en_cours = New-Object System.Windows.Forms.Label
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$totalTCintegration = New-Object System.Windows.Forms.Label
$label4 = New-Object System.Windows.Forms.Label
$listeTCasupprimer = New-Object System.Windows.Forms.ListBox
$boutontransfert = New-Object System.Windows.Forms.Button
$label1 = New-Object System.Windows.Forms.Label
$btnRefresh = New-Object System.Windows.Forms.Button
$TCintegration = New-Object System.Windows.Forms.ListBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#
#Generated Event Script Blocks
#
#Provide Custom Code for events specified in PrimalForms.
$bouton_lancer_la_suppression=
{
bouton_lancer_la_suppression
#TODO: Place custom script here

}

$action_si_click_sur_listedesTCasupprimer=
{
action_si_click_sur_listedesTCasupprimer
#TODO: Place custom script here

}

$handler_label2_Click=
{
#TODO: Place custom script here

}

$action_si_click_sur_TCintegration=
{
action_si_click_sur_TCintegration
#TODO: Place custom script here

}

$bouton_refresh=
{
bouton_refresh
#TODO: Place custom script here

}

$handler_label1_Click=
{

#TODO: Place custom script here

}

$handler_form1_Load=
{
bouton_refresh
#TODO: Place custom script here

}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}

#
#region Generated Form Code
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,185,209,234)
$form1.Text = \"Delete thinclient integration\"
$form1.Name = \"form1\"
$form1.AutoScaleMode = 3

$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.AutoScroll = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 448
$System_Drawing_Size.Height = 594
$form1.ClientSize = $System_Drawing_Size
$form1.add_Load($handler_form1_Load)

$label2.TabIndex = 40
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 396
$System_Drawing_Size.Height = 16
$label2.Size = $System_Drawing_Size
$label2.Text = \" netbios / num serie / ip / mac\"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 24
$System_Drawing_Point.Y = 42
$label2.Location = $System_Drawing_Point
$label2.DataBindings.DefaultDataSourceUpdateMode = 0
$label2.Name = \"label2\"
$label2.add_Click($handler_label2_Click)

$form1.Controls.Add($label2)

$suppression_en_cours.TabIndex = 39
$suppression_en_cours.BackColor = [System.Drawing.Color]::FromArgb(0,255,255,255)
$suppression_en_cours.TextAlign = 32
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 396
$System_Drawing_Size.Height = 29
$suppression_en_cours.Size = $System_Drawing_Size
$suppression_en_cours.Font = New-Object System.Drawing.Font(\"Microsoft Sans Serif\",11.25,1,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 24
$System_Drawing_Point.Y = 547
$suppression_en_cours.Location = $System_Drawing_Point
$suppression_en_cours.DataBindings.DefaultDataSourceUpdateMode = 0
$suppression_en_cours.Name = \"suppression_en_cours\"

$form1.Controls.Add($suppression_en_cours)

$groupBox1.Name = \"groupBox1\"

$groupBox1.Text = \"Total :\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 53
$System_Drawing_Size.Height = 40
$groupBox1.Size = $System_Drawing_Size
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 367
$System_Drawing_Point.Y = 266
$groupBox1.Location = $System_Drawing_Point
$groupBox1.BackColor = [System.Drawing.Color]::FromArgb(255,185,209,234)
$groupBox1.TabStop = $False
$groupBox1.TabIndex = 34
$groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0

$form1.Controls.Add($groupBox1)
$totalTCintegration.TabIndex = 0
$totalTCintegration.TextAlign = 32
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 41
$System_Drawing_Size.Height = 21
$totalTCintegration.Size = $System_Drawing_Size
$totalTCintegration.Font = New-Object System.Drawing.Font(\"Microsoft Sans Serif\",9.75,1,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 6
$System_Drawing_Point.Y = 16
$totalTCintegration.Location = $System_Drawing_Point
$totalTCintegration.DataBindings.DefaultDataSourceUpdateMode = 0
$totalTCintegration.Name = \"totalTCintegration\"

$groupBox1.Controls.Add($totalTCintegration)


$label4.TabIndex = 33
$label4.BackColor = [System.Drawing.Color]::FromArgb(0,255,255,255)
$label4.TextAlign = 2
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 263
$System_Drawing_Size.Height = 25
$label4.Size = $System_Drawing_Size
$label4.Text = \"Liste des pcs à supprimer\"
$label4.Font = New-Object System.Drawing.Font(\"Comic Sans MS\",11.25,1,3,1)
$label4.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 98
$System_Drawing_Point.Y = 327
$label4.Location = $System_Drawing_Point
$label4.DataBindings.DefaultDataSourceUpdateMode = 0
$label4.Name = \"label4\"

$form1.Controls.Add($label4)

$listeTCasupprimer.FormattingEnabled = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 396
$System_Drawing_Size.Height = 134
$listeTCasupprimer.Size = $System_Drawing_Size
$listeTCasupprimer.DataBindings.DefaultDataSourceUpdateMode = 0
$listeTCasupprimer.Name = \"listeTCasupprimer\"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 24
$System_Drawing_Point.Y = 355
$listeTCasupprimer.Location = $System_Drawing_Point
$listeTCasupprimer.TabIndex = 32
$listeTCasupprimer.add_Click($action_si_click_sur_listedesTCasupprimer)

$form1.Controls.Add($listeTCasupprimer)

$boutontransfert.TabIndex = 30
$boutontransfert.Name = \"boutontransfert\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 141
$System_Drawing_Size.Height = 23
$boutontransfert.Size = $System_Drawing_Size
$boutontransfert.UseVisualStyleBackColor = $True

$boutontransfert.Text = \"Lancer le traitement ....\"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 231
$System_Drawing_Point.Y = 509
$boutontransfert.Location = $System_Drawing_Point
$boutontransfert.DataBindings.DefaultDataSourceUpdateMode = 0
$boutontransfert.add_Click($bouton_lancer_la_suppression)

$form1.Controls.Add($boutontransfert)

$label1.TabIndex = 26
$label1.BackColor = [System.Drawing.Color]::FromArgb(0,255,255,255)
$label1.TextAlign = 2
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 199
$System_Drawing_Size.Height = 23
$label1.Size = $System_Drawing_Size
$label1.Text = \"Liste des pcs\"
$label1.Font = New-Object System.Drawing.Font(\"Comic Sans MS\",11.25,1,3,1)
$label1.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 119
$System_Drawing_Point.Y = 9
$label1.Location = $System_Drawing_Point
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$label1.Name = \"label1\"
$label1.add_Click($handler_label1_Click)

$form1.Controls.Add($label1)

$btnRefresh.TabIndex = 8
$btnRefresh.Name = \"btnRefresh\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 130
$System_Drawing_Size.Height = 23
$btnRefresh.Size = $System_Drawing_Size
$btnRefresh.UseVisualStyleBackColor = $True

$btnRefresh.Text = \"REFRESH\"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 57
$System_Drawing_Point.Y = 509
$btnRefresh.Location = $System_Drawing_Point
$btnRefresh.DataBindings.DefaultDataSourceUpdateMode = 0
$btnRefresh.add_Click($bouton_refresh)

$form1.Controls.Add($btnRefresh)

$TCintegration.FormattingEnabled = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 396
$System_Drawing_Size.Height = 199
$TCintegration.Size = $System_Drawing_Size
$TCintegration.DataBindings.DefaultDataSourceUpdateMode = 0
$TCintegration.Name = \"TCintegration\"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 24
$System_Drawing_Point.Y = 61
$TCintegration.Location = $System_Drawing_Point
$TCintegration.TabIndex = 2
$TCintegration.add_Click($action_si_click_sur_TCintegration)

$form1.Controls.Add($TCintegration)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm

[/code:1]

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 10 ans 9 mois #19772 par Laurent Dardenne
Réponse de Laurent Dardenne sur le sujet Re:plantage form
Salut,
gaetanmm2 écrit:

avait vous une solution ?

Pour ce type de développement Powershell est loin d'être le plus simple.
A la rigueur utilise des jobs et le polling , cela reste un pis aller mais le code sera maintenable.
Si tu as le temps tu peux faire du reverse sur ce projet qui me semble le plus proche de ton objectif.

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 10 ans 9 mois #19778 par gaetan
Réponse de gaetan sur le sujet Re:plantage form
Laurent Dardenne écrit:

Salut,
gaetanmm2 écrit:

avait vous une solution ?

Pour ce type de développement Powershell est loin d'être le plus simple.
A la rigueur utilise des jobs et le polling , cela reste un pis aller mais le code sera maintenable.
Si tu as le temps tu peux faire du reverse sur ce projet qui me semble le plus proche de ton objectif.


merci laurent

visiblement il faut que j'utilise les runspace : ex :

powershell.jumbor.fr/2012/12/runspace-22.html

d'apres l'exemple et le test ca fonctionne , par contre j'ai du mal à l'adapté à mon script ...

la logique est qd meme compliqué

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 10 ans 9 mois #19779 par Laurent Dardenne
Réponse de Laurent Dardenne sur le sujet Re:plantage form
gaetanmm2 écrit:

la logique est qd meme compliqué

Non, il s'agit de combiner des conceptions différentes, plutôt des environnements d'exécution qui ne sont pas dans le même thread.

Il y a ici Win32 (windows natif), C# (Winform managé),Powershell (job/runspace).
C'est effectivement relativement complexe, là où voudrait juste effectuer un traitement simple sans se prendre la tête. Raté !

Si tu es le seul utilisateur de ce traitement, le mode console est une option a envisager, quitte à ce que chaque traitement produise un rapport visualisable dans un GridView. L'usage de workflow, si c'est possible dans ton cas, permettrais également un mécanisme de reprise.
Tu peux persévérer dans cette voie, mais cela va te prendre du temps :)

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 10 ans 9 mois #19939 par NicolasBdn
Réponse de NicolasBdn sur le sujet Re:plantage form
Bonjour,

J'ai été confronté aux mêmes problèmes que toi, sauf que je suis passé par du wpf.

Comme tu l'as deviné le problème proviens du faite que le thread qui lance le traitement est le même que celui de l'interface graphique. Il ne peux pas faire deux chose en même temps.

Je suis passé par un runspace avec une table de Hash synchronisée sur les deux runspaces.
Oui car les deux runspaces ne communique pas entre eux.

Voici un article qui m'a bien aidé:

learn-powershell.net/2012/10/14/powershe...-different-runspace/

Voici un bout de code qui pourra t'aider je pense:
[code:1]
### Assembly Necessaire pour la fenêtre graphique ###
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase

### Déclaration de la table de Hashage qui sera synchronisé entre les différents Runspaces
$syncHash = [hashtable]::«»Synchronized(@{})

### Déclaration du premier Runspace pour l'interface Graphique
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = \"STA\" ## Le STA est là pour l'interface en WPF
$newRunspace.ThreadOptions = \"ReuseThread\"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable(\"syncHash\",$syncHash)
$psCmd = [PowerShell]::Create().AddScript({


###### Déclaration du second runspace qui contient les instructions de mise à jour de l'interface graphique. ######
$MAJ_GUIRunspace =[runspacefactory]::CreateRunspace()
$MAJ_GUIRunspace.ApartmentState = \"MTA\"
$MAJ_GUIRunspace.ThreadOptions = \"ReuseThread\"
$MAJ_GUIRunspace.Open()
$MAJ_GUIRunspace.SessionStateProxy.SetVariable(\"syncHash\",$syncHash)

$psScript = [PowerShell]::Create().AddScript({

## Variables du Runspace MAJ
$i = 0

## Corp du runspace
while($i -le 10) {
$global:«»syncHash.Window.Dispatcher.invoke([action]{
$syncHash.progressbar.Value=$($i * 10)
},\"Normal\"«»)
sleep 1
$i++
}
})
## Fin du second Runspace

##Déclaration du code xaml de l'interface graphique WPF du patcher
[xml]$xaml = @\"
<Window
xmlns=\"schemas.microsoft.com/winfx/2006/xaml/presentation\";
xmlns:«»x=\"schemas.microsoft.com/winfx/2006/xaml\";
Title=\"Updater SaveClientNomade\" Height=\"266\" Width=\"598\" MinHeight=\"266\" MinWidth=\"598\">
<Grid Margin=\"0,0,2,0\">
<Button Content=\"Quitter\" Height=\"23\" Margin=\"0,0,196,22\" Name=\"button2\" VerticalAlignment=\"Bottom\" HorizontalAlignment=\"Right\" Width=\"134\" />
<Button Content=\"Vérifier\" Name=\"button3\" HorizontalAlignment=\"Left\" Height=\"23\" Margin=\"340,65,0,0\" VerticalAlignment=\"Top\" Width=\"52\" />
<ProgressBar HorizontalAlignment=\"Left\" Name=\"progressbar1\" Height=\"21\" Margin=\"417,67,0,0\" VerticalAlignment=\"Top\" Width=\"129\"/>
</Grid>
</Window>

\"@

##Intégration du code xaml à la table de Hachage
$reader = New-Object System.Xml.XmlNodeReader $xaml
$global:«»syncHash.Window = [Windows.Markup.XamlReader]::Load( $reader )

## Variables en fonction des formes de l'interface WPF déclaré dans la variable $xaml
$global:«»syncHash.button_quitter = $global:«»syncHash.Window.FindName(\"button2\"«»)
$global:«»syncHash.button_verifier = $global:«»syncHash.Window.FindName(\"button3\"«»)
$global:«»syncHash.progressbar = $global:«»syncHash.Window.FindName(\"progressbar1\"«»)

## Ajout de la posibiliter de quitter le patcher
$syncHash.button_quitter.add_click({
$syncHash.Window.Close() ; break })

## Ajout du second Runspace au bouton vérifier
$syncHash.button_verifier.add_click({
$psScript.Runspace = $MAJ_GUIRunspace
$psScript.BeginInvoke() | Out-Null
})
$syncHash.Window.ShowDialog() | Out-Null
})
$psCmd.Runspace = $newRunspace
$psCmd.BeginInvoke() | Out-Null[/code:1]<br><br>Message édité par: NicolasB, à: 7/06/15 21:37

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.101 secondes
Propulsé par Kunena