Stop Function without exit form

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
Posts: 110
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Fri Sep 16, 2016 4:27 am

Stop Function without exit form

Post by ramses147 »

Hi guys !
I created this simple example form to show my problem.
Once you press the start button, the TestFunction function starts and will execute 50 pings that will be reported in the $ outbox below.
My problem is to stop the function, I would like to set a button to terminate the function WITHOUT exiting the form.
Unfortunately when the ping function is running I can't click on any button, only when it has finished.
Thank you in advance

Code: Select all


[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  

$Form = New-Object System.Windows.Forms.Form    
$Form.Size = New-Object System.Drawing.Size(600,400)  
$Form.StartPosition = "CenterScreen"

Function TestFunction {
$Ping = @()
$count = 50
While ($Count -gt 0){
$ping = Get-WmiObject Win32_PingStatus -Filter "Address = ''" | 
select @{Label="TimeStamp";Expression={Get-Date}},
@{Label="Source";Expression={ $_.__Server }},
@{Label="Destination";Expression={ $_.Address }},
Write-Verbose ($ping | out-string)
$count --
Start-Sleep -Seconds 1

    $i = $Count
    foreach ($item in $Count){
    $text = $Ping.Source, $Ping.Destination, $Ping.IPv4Address, $Ping.Bytes, $Ping."Time(ms)" -join '     '
    $outputBox.AppendText("$text `r`n")

$Button = New-Object System.Windows.Forms.Button 
$Button.Location = New-Object System.Drawing.Size(20,30) 
$Button.Size = New-Object System.Drawing.Size(110,80) 
$Button.Text = "START" 
$Button.BackColor = "LightGreen"
$Button.Cursor = [System.Windows.Forms.Cursors]::Hand
$Button.Font = New-Object System.Drawing.Font("Calibri",11,[System.drawing.FontStyle]::Bold) 

$outputBox = New-Object System.Windows.Forms.TextBox 
$outputBox.Location = New-Object System.Drawing.Size(10,180) 
$outputBox.Size = New-Object System.Drawing.Size(565,50) 
$outputBox.MultiLine = $True 
$outputBox.SelectionStart = $outputBox.Text.Length;
$outputBox.ScrollToCaret() = $true
$outputBox.ScrollBars = "Vertical"

[void] $Form.ShowDialog()

User avatar
Site Admin
Posts: 399
Joined: Sun Mar 03, 2013 12:42 pm

Re: Stop Function without exit form

Post by mxtrinidad »

Check this example.

$count = 10; $i = 1;
While ($i -le $count)
Write-Host "Print $i";

Hope this helps!


User avatar
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: Stop Function without exit form

Post by davidc »

This article may be of assistance to you: ... sive-loops

As well as this one: ... sive-forms

If you want to use ping.exe as an alternative, you can use the redirect process output template: ... -v5-5-155/
SAPIEN Technologies, Inc.