0

I have cmd script. I want to use timer for 10 second to give a decision to continue cmd script process or to pause it. I want to put this script at the first line of my cmd script

powershell.exe -ExecutionPolicy Bypass -File %~dp0\Pause_GUI.ps1

It will pop up 10s countdown, after 10s, it will continue the cmd script process by return errorlevel, but if we click button pause, the cmd script will pause, also by return errorlevel. Anyone can help please

UPDATED

#------------------------------------------- Add in Forms Controls -------------------------------------------#
Add-Type -AssemblyName System.Windows.Forms
#-------------------------------------------------------------------------------------------------------------#


#---------------------------------------- Begins creation of the form ----------------------------------------#
$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text = "Message"
$MainForm.Width = 500
$MainForm.Height = 200
$MainForm.StartPosition = "CenterScreen"
$MainForm.BackColor = "#e2e2e2"
#-------------------------------------------------------------------------------------------------------------#


#----------------------------------------------- Button Clicks -----------------------------------------------#
$Auto_Button = ({ $global:result=1
                  $MainForm.Close() })
$Manual_Button = ({ $global:result=0
                    $MainForm.Close() })
#-------------------------------------------------------------------------------------------------------------#

#-------------------------------------------------- Buttons --------------------------------------------------#
$Automatic = New-Object System.Windows.Forms.Button
$Automatic.Location = New-Object System.Drawing.Size(110,80)
$Automatic.Size = New-Object System.Drawing.Size(120,30)
$Automatic.Text = "Continue After 10s"
$Automatic.BackColor = "#e47104"
$Automatic.Add_Click($Auto_Button)
$MainForm.Controls.Add($Automatic)

$Manual = New-Object System.Windows.Forms.Button
$Manual.Location = New-Object System.Drawing.Size(270,80)
$Manual.Size = New-Object System.Drawing.Size(100,30)
$Manual.Text = "Pause"
$Manual.BackColor = "#e47104"
$Manual.Add_Click($Manual_Button)
$MainForm.Controls.Add($Manual)
#-------------------------------------------------------------------------------------------------------------#

#--------------------------------------------- Displays the Form ---------------------------------------------#
$result=0
$MainForm.ShowDialog()
exit $result
#-------------------------------------------------------------------------------------------------------------#

How to handle the button "Continue after 10s" as a timer? And the GUI will close automatically after 10s

3
  • Where is your button object? Where is the Timer type that has been added? perhaps looking at adding the timer and playing around with the methods of that. $Timer = New-Object -Type Timers.Timer Commented Jun 21, 2019 at 2:24
  • Missed the button object entirely, my bad. You will need to add the timer and have a play around with a countdown and the Add_Tick method. Commented Jun 21, 2019 at 2:35
  • I updated my code, Could you please help how do I control the button? Commented Jun 21, 2019 at 3:34

1 Answer 1

1

You need a System.Windows.Forms.Timer-object that counts your time and a .tick-event that will trigger when the time has come. However you need to stop (and dispose) the timer or it will keep on triggering the event even when the window is closed. (In Powershell ISE that could cause windows to close as soon as you load them). To grab the timer from within it's own event you need to adress it in the right scope. I used the global-scope for that.

$Auto_Button = ({ 
                    $global:Counter = 0
                    $global:timer = New-Object -type System.Windows.Forms.Timer
                    $global:timer.Interval = 1000
                    $global:timer.add_Tick({
                        if ($Counter -eq 10){
                            write-host $global:counter
                            $global:timer.Stop()
                            $global:timer.Dispose()
                            $result=1
                            $MainForm.Close()                    
                            $global:Counter++
                        }else{
                            write-host $global:counter
                            $global:Counter++
                        }
                    })
                    $global:timer.Start()
               })
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. But It does not what I expect for. Your answers I have to click the button first, then the timer will start. But my expectation, when I execute the script, the timer for 10s will start automatically without clicking the button first.
@Job Just omit the $Auto_Button = ({ }). Place $global:timer.Start() where you want to start the countdown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.