Question Refresh automatique dans une interface graphique

Plus d'informations
il y a 12 ans 10 mois #14828 par Burgun
Bonjour,

J'utilise deux solutions de supervision et je souhaitais faire un petit popup regroupant le total de des alertes remontés par les deux produits.

J'ai utilisé PrimalForm pour constuire l'interface et deux petites fonctions pour remonter les infos que je souhaite. Lorsque que je clique sur le bouton refresh, l'information se met à jour.

La ou je bloque c'est que j'aimerais que ce refresh se fasse automatiquement toutes les 30 secondes. L'utilisateur a donc le choix d'attendre la mise à jour ou de faire un refresh forcé.

Je pensais faire un simple do while, sur une condition toujours vrais. Mais lorsque je fais ceci, je n'ai aucun message d'erreur mais l'interface se fige et l'info ne semble pas se mettre à jour.

Voici les actions réalisés lorsque j'appuis sur refresh

[code:1]
$button1_OnClick=
{

$AlerteDansZabbix=Check-zabbix -Srv \"MONSERVEUR\"
$AlerteDansSCOM=Check-SCOM -Srv \"MONSERVEUR2\"

$TotalAlert=$AlerteDansZabbix+$AlerteDansSCOM

if ($TotalAlert -eq 0)
{
$Meteo.Image = [System.Drawing.Image]::Fromfile($fileok)
}
else
{
$file=
$Meteo.Image = [System.Drawing.Image]::Fromfile($fileko)
}


$AffAlerteScom.Text=$AlerteDansSCOM
$AalertZab.Text=$AlerteDansZabbix
#Start-Sleep -Seconds 30
}

[/code:1]

Si quelqu'un peu m'aiguiller pour rendre ce refresh automatique ?

Voici le script complet

[code:1]###DashBoard Alert
###VBU
###V1.0 du 14/05

###Importation des modules
Import-Module OperationsManager
[void] [system.reflection.Assembly]::LoadWithPartialName(\"MySql.Data\"«»)


$fileko=\"c:\temp\KO.gif\"

$fileok=\"c:\temp\OK.gif\"



####Fonctions

#SCOM
function Check-SCOM ([String]$Srv)
{
New-SCOMManagementGroupConnection $Srv
#Set-Location ‘OperationsManagerMonitoring::’

#Récupéation des alertes
#Get-Alert | where {$_.ResolutionState –eq 0} | select-object MonitoringObjectDisplayName, Name, ResolutionState, RepeatCount | sort Name | export-csv $Bilan -noTypeInformation
$Alerts=Get-SCOMAlert | where {$_.ResolutionState –eq 0} | select-object MonitoringObjectDisplayName, Name, ResolutionState, RepeatCount | sort Name
$NbrAlertScom=($Alerts.count)
return $NbrAlertScom
}

#Zabbix
function Check-zabbix ([String]$Srv)
{
$serv = \"$Srv\"
$port = \"3306\"
$user = \"zabbixo\"
$password = \"mdp\"
$db = \"zabbix\"


# Creation de l'instance, connexion à la base de donnees
$mysql = New-Object MySql.Data.MySqlClient.MySqlConnection(\"server=$serv;port=$port;uid=$user;pwd=$password;database=$db;Pooling=False\"«»)
$mysql.Open()

# Recuperatation
$reqStr = \"SELECT * FROM triggers WHERE value = 1\"
$req = New-Object Mysql.Data.MysqlClient.MySqlCommand($reqStr,$mysql)
# Creation du data adapter et du dataset qui permettront de traiter les donnees
$dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($req)
$dataSet = New-Object System.Data.DataSet
$dataAdapter.Fill($dataSet,\"test\"«»)

####### Interpretation des resultats
$res = $dataSet.Tables[\"test\"]
$i=0
foreach ($Alert in $res)
{
$i++ > $null
}
$NbrAlertZabbix = $i
$mysql.Close()
#return $NbrAlertZabbix
}


#Generated Form Function
function GenerateForm {


#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
$linkLabel2 = New-Object System.Windows.Forms.LinkLabel
$linkLabel1 = New-Object System.Windows.Forms.LinkLabel
$button1 = New-Object System.Windows.Forms.Button
$Meteo = New-Object System.Windows.Forms.PictureBox
$AalertZab = New-Object System.Windows.Forms.TextBox
$AffAlerteScom = New-Object System.Windows.Forms.TextBox
$AlertZabbix = New-Object System.Windows.Forms.Label
$AlertScom = New-Object System.Windows.Forms.Label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#
#Generated Event Script Blocks
#
#Provide Custom Code for events specified in PrimalForms.
$button1_OnClick=
{

$AlerteDansZabbix=Check-zabbix -Srv \"Serveur1\"
$AlerteDansSCOM=Check-SCOM -Srv \"Serveur2\"

$TotalAlert=$AlerteDansZabbix+$AlerteDansSCOM

if ($TotalAlert -eq 0)
{
$Meteo.Image = [System.Drawing.Image]::Fromfile($fileok)
}
else
{
$file=
$Meteo.Image = [System.Drawing.Image]::Fromfile($fileko)
}


$AffAlerteScom.Text=$AlerteDansSCOM
$AalertZab.Text=$AlerteDansZabbix
#Start-Sleep -Seconds 30
}




$handler_label3_Click=
{
#TODO: Place custom script here

}

$handler_AalertZab_TextChanged=
{
#TODO: Place custom script here

}

$handler_label1_Click=
{
#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
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 104
$System_Drawing_Size.Width = 256
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = \"form1\"
$form1.Text = \"Alertes Dashboard\"

$linkLabel2.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 196
$System_Drawing_Point.Y = 39
$linkLabel2.Location = $System_Drawing_Point
$linkLabel2.Name = \"linkLabel2\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 56
$linkLabel2.Size = $System_Drawing_Size
$linkLabel2.TabIndex = 7
$linkLabel2.TabStop = $True
$linkLabel2.Text = \"Zabbix\"

$form1.Controls.Add($linkLabel2)

$linkLabel1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 196
$System_Drawing_Point.Y = 6
$linkLabel1.Location = $System_Drawing_Point
$linkLabel1.Name = \"linkLabel1\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 48
$linkLabel1.Size = $System_Drawing_Size
$linkLabel1.TabIndex = 6
$linkLabel1.TabStop = $True
$linkLabel1.Text = \"SCOM\"

$form1.Controls.Add($linkLabel1)


$button1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 68
$button1.Location = $System_Drawing_Point
$button1.Name = \"button1\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$button1.Size = $System_Drawing_Size
$button1.TabIndex = 5
$button1.Text = \"Refresh\"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($button1_OnClick)

$form1.Controls.Add($button1)

$Meteo.DataBindings.DefaultDataSourceUpdateMode = 0


$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 118
$System_Drawing_Point.Y = 67
$Meteo.Location = $System_Drawing_Point
$Meteo.Name = \"Meteo\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 42
$System_Drawing_Size.Width = 51
$Meteo.Size = $System_Drawing_Size
$Meteo.TabIndex = 4
$Meteo.TabStop = $False

$form1.Controls.Add($Meteo)

$AalertZab.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 118
$System_Drawing_Point.Y = 39
$AalertZab.Location = $System_Drawing_Point
$AalertZab.Name = \"AalertZab\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 51
$AalertZab.Size = $System_Drawing_Size
$AalertZab.TabIndex = 3
$AalertZab.add_TextChanged($handler_AalertZab_TextChanged)

$form1.Controls.Add($AalertZab)

$AffAlerteScom.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 119
$System_Drawing_Point.Y = 9
$AffAlerteScom.Location = $System_Drawing_Point
$AffAlerteScom.Name = \"AffAlerteScom\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 50
$AffAlerteScom.Size = $System_Drawing_Size
$AffAlerteScom.TabIndex = 2

$form1.Controls.Add($AffAlerteScom)

$AlertZabbix.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 42
$AlertZabbix.Location = $System_Drawing_Point
$AlertZabbix.Name = \"AlertZabbix\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 100
$AlertZabbix.Size = $System_Drawing_Size
$AlertZabbix.TabIndex = 1
$AlertZabbix.Text = \"Alertes Zabbix\"
$AlertZabbix.add_Click($handler_label3_Click)

$form1.Controls.Add($AlertZabbix)

$AlertScom.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 9
$AlertScom.Location = $System_Drawing_Point
$AlertScom.Name = \"AlertScom\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 100
$AlertScom.Size = $System_Drawing_Size
$AlertScom.TabIndex = 0
$AlertScom.Text = \"Alertes SCOM\"
$AlertScom.add_Click($handler_label1_Click)

$form1.Controls.Add($AlertScom)

#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]<br><br>Message édité par: bvivi57, à: 14/05/13 14:18

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

Plus d'informations
il y a 12 ans 10 mois #14832 par Burgun
Je pense etre arrivé à quelque chose en faisant connaissance avec l'objet \&quot;System.Windows.Forms.Timer\&quot;

J'ai pas très bien compris comment cela fonctionne, mais en ajoutant

[code:1]$timer1 = New-Object System.Windows.Forms.Timer[/code:1]

avec une définition de l'objet :
[code:1]$timer1.Interval = 30000
$timer1.add_tick($timer1_OnTick)[/code:1]

et l'action sur le timer

[code:1]$timer1_OnTick =
{
$AlerteDansZabbix=Check-zabbix -Srv \&quot;SRV1\&quot;
$AlerteDansSCOM=Check-SCOM -Srv \&quot;SRV2\&quot;

$TotalAlert=$AlerteDansZabbix+$AlerteDansSCOM

if ($TotalAlert -ne 0)
{
$Meteo.Image = [System.Drawing.Image]::Fromfile($fileok)
}
else
{
$file=$Meteo.Image = [System.Drawing.Image]::Fromfile($fileko)
}

}[/code:1]


puis lorsque l'on cliquer sur le bouton refresh j'ai ajouté ces actions

[code:1]$timer1.Enabled = $true
$timer1.Start()[/code:1]

La ou je m'étonne c'est que je pensais que le refresh se ferait une seule fois, mais l'information semble bien se rafraichir toutes les 30s.

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

Plus d'informations
il y a 12 ans 10 mois #14834 par Laurent Dardenne
Salut,
bvivi57 écrit:

Si quelqu'un peu m'aiguiller pour rendre ce refresh automatique ?

Le terme \&quot;refresh\&quot; que tu utilises est plutôt dédié à l'interface (GUI). Tes composants sont liés à des collections de données, aborde ton problème différemment, par exemple avec le databinding.

La mise à jour de la collection cible entraînera celle de l'interface.

Tutoriels PowerShell

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

Plus d'informations
il y a 12 ans 10 mois #14845 par Burgun
Merci pour cette remarque.

Je vais creuser le sujet.

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

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