Shut down Windows programmatically and wait for termination of resident processes - windows

Background
I am writing a program to shut down Windows at the specified time. I am using shutdown.exe to implement this function.
In my computer, there is a resident process to record system activity and send the record to a server when shutting down Windows. When you shut down Windows from the start menu (RuntimeBroker.exe), Windows waits for the resident process to finish sending the record (according to HKCU\Control Panel\Desktop\HungAppTimeout).
Problem
When I shut down Windows with shutdown.exe, it doesn't refer to HKCU\Control Panel\Desktop\HungAppTimeout and kills the resident process forcibly before it finishes sending the record.
Question
Is there any way (like a PowerShell cmdlet or a command included in Windows) to shut down Windows programmatically and wait for the termination of the resident processes gracefully before the shutdown?

Based on #An-dir's advice, I have tested some ways to shutdown Windows:
#
How to shutdown
What is executed
Wait for HungAppTimeout
1
shutdown from Start Menu
RuntimeBroker.exe
Yes
2
run shutdown.exe
shutdown.exe
No
3
run SlideToShutDown.exe
SlideToShutDown.exe
Yes
4
run Stop-Computer cmdlet
WmiPrvSE.exe
Yes
5
shut down from the lock screen
winlogon.exe
No
6
Alt+F4 on Desktop
Explorer.exe
Yes
For my purpose, Stop-Computer and SlideToShutDown.exe look suitable.

Related

Windows execute command before sleep / on power button press

I want my power button to send a HTTP request before going to sleep.
I already set a task to execute with trigger System/Kernel-Power/42 but the task executes after the next wake rather than before sleep.
Also autohotkey doesn't seem to log the power button neither does a windows trigger.
Is it possible to delay windows sleep? I could not find a way to do that.
Thanks
So it's not a real answer but I came up with a solution:
If the wifi hotspot is turned on (I need it anyways) while my pc goes into sleep mode, it sends a System/Kernel-Power/59 and turns the monitor off.
I now have a task that listens for this and executes what I want, then turns the hotspot off and then turns the pc into sleep mode.

Freeze keyboard and mouse on Windows

I want to create a program for a computer lab. The computers in lab are running Windows 7 and are connected via LAN. The program should freeze keyboard and mouse when PC gets disconnected from LAN and also show a fullscreen message.
Currently I have created the first version of this program that works almost fine. It runs with startup. But the problem is that the users can kill it from Task Manager. I've tried running it as a windows service and it runs but cannot interact with desktop, so it cannot freeze the keyboard and mouse.
Any suggestions is appreciated. Thanks in advance.
So I finally solved it.
The reason my program could not interact with desktop is that it runs in Session 0.
This is because the Windows services run in session 0 and cannot directly interact with desktop. Here by interaction I mean receiving keyboard, mouse events and creating or accessing windows in user's desktop.
So I created a service that runs with SYSTEM account. It will create the "freezer process" in user's session. Also this process will run with SYSTEM account, so the normal user will not have privileges to kill.

Does command line windows restart gracefully exit running app

Platform: Windows 2008 server
Ran into a problem where my one-file exe generated with pyinstaller would not clear its associated temporary folder. Read up about this http://pythonhosted.org/PyInstaller/ and found out if the exe doesn't close gracefully it would not delete its associated temporary folder. After this I spoke to the IT guys who setup the exe and was told that the app is started on system start-up and every night a system reboot occurs.
I tried finding out if the system restart on windows would gracefully exit running applications, but couldn't find anything regarding this.
Does anyone know if windows gracefully exits running apps with a scheduled system restart?
Thanks in advance
Yes, it does, but implementation slightly differs between XP\Vista. You can also change shutdown timer and auto-close behaviour: How to Specify WaitToKillAppTimeout to Speed Up Shut Down Time in Windows.
In Windows XP:
In Windows XP, each running application is sent the WM_QUERYENDSESSION
message at shutdown. Applications can return TRUE to indicate that
they can be closed, or FALSE to indicate that they should not be
closed (e.g., because doing so would cause the user to lose data or
destroy a CD being burned). If an application returns FALSE, in most
cases, shutdown will be cancelled (and the application that cancelled
shutdown is sent WM_ENDSESSION with wParam == FALSE).
Applications can also delay responding to WM_QUERYENDSESSION in order
to display UI asking what the user would like to do. For example, when
Notepad has unsaved data and displays a "Would you like to save your
data?" dialog during shutdown, this is what it is doing. By default,
applications can delay responding to WM_QUERYENDSESSION for up to 5
seconds. After 5 seconds, Windows XP will display a dialog that
indicates that the application is not responding and allows the user
to terminate it. Until the user responds to this dialog, applications
can block WM_QUERYENDSESSION (and, consequently, shutdown)
indefinitely.
In Windows Vista
Ability for users to forcefully shut down
In Windows XP, the UI for
blocking applications allows users to either cancel shutdown or
terminate the blocking application. If subsequent applications also
block shutdown, the system displays identical UI for each blocking
application. This is frustrating for many users, who, when shutting
down, "just want" their computers to turn off. Windows Vista will
solve this by allowing users to terminate the blocking application and
make shutdown "forceful." In a forceful shutdown, Windows will send
applications WM_QUERYENDSESSION with the ENDSESSION_CRITICAL flag. If
an application responds FALSE, Windows will continue shutdown instead
of canceling it, and will send the application WM_ENDSESSION. If an
application times out responding to WM_QUERYENDSESSION or
WM_ENDSESSION, Windows will terminate it.
Silent shutdown cancellations will no longer be allowed
In Windows XP,
applications are allowed to veto WM_QUERYENDSESSION without displaying
any UI indicating why they need to cancel shutdown. These "silent
shutdown failures" are highly frustrating to users, who often take a
minute or two to realize that shutdown has failed because no UI was
displayed. Windows Vista will eliminate this possibility by displaying
UI even if an application vetoes WM_QUERYENDSESSION.
Certain types of applications will no longer be allowed to block
shutdown. At shutdown, Windows Vista will check whether each running
application is not responding (an application is defined as not
responding if it has not responded to any of its window messages in
the last 5 seconds), and, if so, automatically terminate it.
Windows Vista will also not allow console applications or applications
that have no visible top-level windows to block shutdown. In most
cases, such applications are less important to users at shutdown than
applications that do have visible top-level windows. If an application
without a visible top-level window blocks shutdown by vetoing
WM_QUERYENDSESSION, or takes over 5 seconds to respond to
WM_QUERYENDSESSION or WM_ENDSESSION, Windows will automatically
terminate it.
However, if an application with no visible top-level windows uses the
new API to proactively indicate that it needs to block shutdown,
Windows Vista will not automatically terminate it, and will instead
treat it like an application that does have a visible top-level
window.

EC2 windows instance as dev machine, need to auto-shutdown after X period of user inactivity

I want to use an EC2 windows instance as an development environment, and want it to shutdown automatically when there is X period of user inactivity (either no remote desktop connection or idle RD connection), so as to minimize costs. Any way to do this?
You can use task scheduler inside of the windows machine to set up a trigger that will shut down the machine when it is inactive.
open the task scheduler (assuming windows 7 or 8 just type it in to
search for it in the start/search box)
on the right actions box create a task
give it a name
Run whether user is logged on or not
Run with highest privileges (shutdown requires admin rights)
Click Triggers tab
New...
Begin the task: set to on idle
click ok
click conditions tab
Check mark start the task only if the computer is idle for (your
amount of idle time)
check restart if the idle state resumes
Click the Actions Tab
New...
program = cmd.exe
add arguments = shutdown /s /f
Should be good to go after that

How do i capture each time the Operating System OR the Active Window is busy

I am trying to capture Windows system busy, or Active window busy, from the perspective of the end user (typically when the Windows OS or the active Window puts up a Busy Cursor: or to capture when the system is busy). I am trying to do this without having to continually use GetCursor. Is there any way to set up a windows interrupt, such that when the Operating System or the the active Window is busy that and interrupt can be generated? thanks for your help
You could perform monitoring of the ProcessorQueueLength using the Win32_PerfFormattedData_PerfOS_System WMI object. There are various other performance counter, use the perfmon tool to investigate which of these are most useful to you.

Resources