- 1 year ago
C#: Check if the shutdown /t xx timer ist active
In C#, you can check if the shutdown timer (shutdown /t xx
) is active by reading the value of the WaitToKillAppTimeout
registry key. This key indicates the time, in milliseconds, that Windows waits for applications to shut down before forcefully terminating them during a system shutdown. If the value is greater than zero, it means a shutdown timer is active.
Here's an example code snippet to check the shutdown timer status in C#:
csharpusing Microsoft.Win32;
public bool IsShutdownTimerActive()
{
const string keyPath = @"HKEY_CURRENT_USER\Control Panel\Desktop";
const string valueName = "WaitToKillAppTimeout";
int waitToKillAppTimeout = (int)Registry.GetValue(keyPath, valueName, 0);
return waitToKillAppTimeout > 0;
}
In the code above, Registry.GetValue
retrieves the value of the WaitToKillAppTimeout
registry key located in HKEY_CURRENT_USER\Control Panel\Desktop
. If the value is greater than zero, it means the shutdown timer is active.
Make sure to include the Microsoft.Win32
namespace for accessing the registry-related classes and methods.
Note that the above code checks the registry value under the current user's settings. If you need to check the system-wide shutdown timer, you may need to modify the keyPath
to point to the appropriate registry location (HKEY_LOCAL_MACHINE
) and ensure the application has sufficient permissions to read the registry.
Remember to handle exceptions or potential null values when accessing the registry to ensure robustness in your code.