Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

PowerShell GUI Problems - MultiThreading & Freezing GUI

$
0
0

Hi,

When i run my powershell script (its has GUI) the form freez and do you have any idea how i can avoid it?

I have added [System.Windows.Forms.Application]::DoEvents() but it still not working. 

 

Please help :-)

try
	{[System.Windows.Forms.Application]::DoEvents()$buttonStart.Enabled = $false$buttonStop.Enabled = $false$buttonRefresh.Enabled = $false$LabelStatus.Text = ""$richtextboxInfo.Text = ""$WebEsServices1 = List-Services
		Load-DataGridView -DataGridView $datagridview1 -Item $WebEsServices1$MemWMI = Get-WmiObject -Class Win32_Operatingsystem -ComputerName $global:Servername -Credential $Global:Credential$TotalMemory = [Math]::Round($MemWMI.totalVisibleMemorySize / 1mb, 2)$RemMemory = [Math]::Round(($TotalMemory - 4) * 1GB / 1MB, 2)#$FreeMemory = [Math]::Round($MemWMI.FreePhysicalMemory / 1mb, 2)$richtextboxInfo.AppendText("Total Memory: $TotalMemory GB`n")$richtextboxInfo.AppendText("`n")$Pro = Invoke-Command -ComputerName $global:Servername -Credential $Global:Credential -ScriptBlock { Get-Process -Name Java* -ErrorAction 'Stop' }$TotalJavaMem = 0foreach ($itemin$Pro)
		{[System.Windows.Forms.Application]::DoEvents()$Storlek = [Math]::Round($item.workingset64 / 1mb)$richtextboxInfo.AppendText("$($item.ProcessName): $Storlek MB`n")$TotalJavaMem += $Storlek
		}$RemainingTotalJavaMem = [Math]::Round(($RemMemory - $TotalJavaMem))$richtextboxInfo.AppendText("`n")$richtextboxInfo.AppendText("Used Memory: $TotalJavaMem MB")$richtextboxInfo.AppendText("`n")$richtextboxInfo.AppendText("Remaining Memory: $RemainingTotalJavaMem MB")if ($RemainingTotalJavaMem -le 0)
		{$LabelStatus.ForeColor = 'Red'$LabelStatus.Text = "Not OK"
		}else
		{$LabelStatus.ForeColor = 'Green'$LabelStatus.Text = "OK"
		}
	}catch[System.Net.WebException], [System.Exception]
	{$ErrorMSg = $_.Exception.Message#[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")[void][System.Windows.Forms.MessageBox]::Show("$ErrorMsg", "Error!")
	}$buttonStart.Enabled = $true$buttonStop.Enabled = $true$buttonRefresh.Enabled = $true

Viewing all articles
Browse latest Browse all 6937

Trending Articles