script stays in taskman

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: 237
Last visit: Thu Jun 13, 2019 7:31 am

script stays in taskman

Post by mqh777 »

I have a form that is compiled into an .EXE. It displays a message saying your system will reboot in 5 min, please close your apps. I've run this on a few different machines and on 2 of them after the reboot this compiled .exe was STILL showing as a Process in TaskMan and the machine kept rebooting ever 5 min. Why? what in this code would cause that behavior?

Code: Select all

#  This will create a countdown timer on the form
	$labelTime.Text = "{0:D2}" -f $TotalTime

# Set countdown to 3 minutes or 180 seconds
$TotalTime = 300

$script:StartTime = (Get-Date).AddSeconds($TotalTime)

	#Use Get-Date for Time Accuracy
	[TimeSpan]$span = $script:StartTime - (Get-Date)
	#Update the display
	$formRebootSystem.Text = $labelTime.Text = "{0:N0}" -f $span.TotalSeconds
	if($span.TotalSeconds -le 0)
		# Once 300 seconds have gone by reboot the system	
		Restart-Computer localhost -Force

	#TODO: Place custom script here

	#TODO: Place custom script here

	#TODO: Place custom script here

Posts: 14759
Last visit: Thu Apr 15, 2021 5:14 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 times

Re: script stays in taskman

Post by jvierra »

Windows 10 remembers an loads processes that were running when the system shut down. They are reloaded when th system restarts. Place the timer.Start command outside of an event will likely cause this behavior.

Placing the timer.start() call in the "Shown" event of the form may prevent this.