Click here to Skip to main content
15,861,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one simple c# application which runs under logged in user. I want to close this app when user tries to switch user and re run it when user is logged in as a same or different user.

What I have tried:

1. I tried adding 1 thread which will run continuously and in this thread :
private static void SwitchUserThread()
        {
            SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SessionSwitchHandler);
            while (true)
            { }
        }


            private static void SessionSwitchHandler(object sender, SessionSwitchEventArgs args)
{
    try
    {
                WriteToLog(DateTime.Now + " : Inside SessionSwitch, args.Reason : " + args.Reason);
                switch (args.Reason)
                {
                    case SessionSwitchReason.ConsoleDisconnect:
                        WriteToLog(DateTime.Now + " : Inside SessionSwitch, args.Reason : " + args.Reason);
                        Application.Exit();
                        WriteToLog(DateTime.Now + " Application Exit ");
                        break;
                    case SessionSwitchReason.ConsoleConnect:
                        WriteToLog(DateTime.Now + " : Inside SessionSwitch, args.Reason : " + args.Reason);
                        Application.Run();
                        WriteToLog(DateTime.Now + " Application started Succefully ");
                        break;
                }
    }
    catch (Exception ex)
    {

    }
}
Posted
Updated 25-Sep-17 2:49am
Comments
Kornfeld Eliyahu Peter 25-Sep-17 8:48am    
Why application and not service?
https://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents(v=vs.110).aspx
Dave Kreskowiak 25-Sep-17 9:07am    
Define what you mean by "when users tries to switch user". Are you talking about if a users logs off Windows and logs back in as someone else?
uday vichare 25-Sep-17 9:12am    
not logging off and on.. I meant just switching user with (win+l) which does not completely logs off previous user.
Kornfeld Eliyahu Peter 25-Sep-17 9:18am    
I can't see the problem... When you use fast user switch the application running in the context of user A will be put aside and user B will have a new environment with a new instance of the application (like an open Word of user A will not appear for user B). All you need is to put your application to the start-up folder (or registry) and you should be OK...
uday vichare 25-Sep-17 9:30am    
I cannot have multiple instances of my application running at the same time.

You can use Application.Restart() Application.Restart Method (System.Windows.Forms)[^]
But I would recommend not doing this from a thread, but e.g. in Program.
 
Share this answer
 
Comments
uday vichare 25-Sep-17 9:03am    
This doesnt work coz restart will restart the application in the same user context as before. i want to launch it in the new user context. for that i have to end the app in the previous user context on switch user event and re launch it in the new user context.
Why can't you run multiple instances? Your "requirement" is very strange.

Perhaps your app needs to be split into a service that handles all the user-independent stuff and is only ever running one instance and another app that runs as the users on the machine and talks to the service.

There is a system event you can subscribe to SystemEvents.SessionSwitch Event (Microsoft.Win32)[^].

The problem is your application can shut itself down and quit, but it cannot restart itself as the user that currently has the active session.

You do NOT get the user of the session that is active or being deactivated. What you do get is a Reason the event was raised in the eventargs. The values of which are here[^].

You would need a second application that runs as the users from the registry Run key and subscribes to the SessionSwitch event. It would have to determine when to launch your single instance application. This includes launching the application when your "monitor" application is launched, tell it to quit itself when a ConsoleDisconnect is detected and relaunching it when ConsoleConnect is seen.

You're going to have to write an app to test what values you get in the event args when certain things happen so you can cover every possibility.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900