I want my Win11 c# program to get activated automatically when user logs in. I didn't find anything documented for this behavior, but I verified using logs that Windows is running the program very early, without waiting for the user to log on.
In task scheduler I verified:
- security options are set with "Run only when user is logged in".
- the trigger is set with "At log on"
I tried several machines, and have seen that this is not always the case. On some machines it waits for the login, in others it won't.
I am looking for a way in code to verify that a user is actually logged in.
Already tried:
Reading Session ID:
int sessionId = Process.GetCurrentProcess().SessionId;
I always get non-zero, so not useful
using WMI to read UserName - it gets filled with username before I log in.
I'll appreciate any help with this!
I want my Win11 c# program to get activated automatically when user logs in. I didn't find anything documented for this behavior, but I verified using logs that Windows is running the program very early, without waiting for the user to log on.
In task scheduler I verified:
- security options are set with "Run only when user is logged in".
- the trigger is set with "At log on"
I tried several machines, and have seen that this is not always the case. On some machines it waits for the login, in others it won't.
I am looking for a way in code to verify that a user is actually logged in.
Already tried:
Reading Session ID:
int sessionId = Process.GetCurrentProcess().SessionId;
I always get non-zero, so not useful
using WMI to read UserName - it gets filled with username before I log in.
I'll appreciate any help with this!
Share Improve this question asked Mar 4 at 10:29 ishahakishahak 6,7956 gold badges41 silver badges59 bronze badges 12- 1 Wouldn't it seem reasonable to first understand the issue before rushing ahead to implement a workaround that's very likely to introduce more problems? – IInspectable Commented Mar 4 at 11:37
- 2 Add shortcut to Startup folder in Start button. – i486 Commented Mar 4 at 12:20
- 1 This is possible TBAL. You can check this in next way. Before shutdown - logout from current user. And reboot. I guess will be no auto start your task in this case. And when you reboot with user active - will be autologon and start your task – RbMm Commented Mar 4 at 12:42
- 1 Does this help? stackoverflow/questions/674628/… – Rufus L Commented Mar 4 at 15:48
- 1 Trusted Boot Auto-Logon (TBAL) - system save credentials for loged user during shutdown. And on boot read this credentials and do auto logon. And then do lock. As result when you "logon" on startup, you really not logon, but unlock system. Explorer already run at this time, despite you yet on winlogon desktop. Task registered for run on logon, also already started. And how i say, you can simply chech this - if you logoff before shutdown, will be no auto logon on boot next time. If you shutdown without logoff - will be auto logon for last user. So you can view different. – RbMm Commented Mar 5 at 9:23
1 Answer
Reset to default 0Here is an answer to my post, based on comments of @RbMm.
Understanding the Early Task Trigger: The Role of Trusted Boot Auto-Logon (TBAL)
When Windows shuts down without a full logoff, it saves the current user's credentials. On startup, TBAL auto-logs you in (with the session locked), which triggers tasks set for "At log on" immediately.
How TBAL Works
Credential Saving:
Shutting down without logging off saves your credentials.Auto Logon:
On boot, Windows uses these credentials to log you in and locks the session.Task Triggering:
Since the environment (includingExplorer
) starts during auto-logon, tasks scheduled for "At log on" run right away.
Summary
TBAL auto-logs in the last user during boot, triggering "At log on" tasks early. This explains why tasks are activated before a full, manual logon.