切换用户时重新启动C#windows应用程序。 [英] Restart the C# windows application when user is switched.

查看:67
本文介绍了切换用户时重新启动C#windows应用程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的c#应用程序,它在登录用户下运行。我想在用户尝试切换用户时关闭此应用,并在用户以相同或不同的用户身份登录时重新运行。



我尝试了什么:



1.我尝试添加1个线程,该线程将在此线程中连续运行:

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)
    {

    }
}

推荐答案

您可以使用 Application.Restart() Application.Restart Method(System.Windows.Forms) [ ^ ]

但我建议不要从一个帖子做这个,但是例如在程序中。
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.


为什么你不能运行多个实例?你的要求很奇怪。



也许你的应用程序需要分成一个服务来处理所有与用户无关的东西,并且只运行一个实例另一个应用程序,作为计算机上的用户运行并与服务进行通信。



有一个系统事件,您可以订阅SystemEvents.SessionSwitch事件(Microsoft.Win32) [ ^ ] 。



问题是您的应用程序可能会自行关闭并退出,但它无法以当前具有活动会话的用户身份重新启动。



您不会获得活动或被停用的会话的用户。你得到的是事件在事件中被提出的原因。其值为此处 [ ^ ]。



您需要第二个应用程序作为用户从注册表Run键运行并订阅SessionSwitch事件。它必须确定何时启动单实例应用程序。这包括在启动监视器应用程序时启动应用程序,告诉它在检测到ConsoleDisconnect时退出,并在看到ConsoleConnect时重新启动它。



你'我必须编写一个应用来测试你在事件args中获得的值,当某些事情发生时,你可以涵盖所有可能性。
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.


这篇关于切换用户时重新启动C#windows应用程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆