如何判断我的应用程序是否在RDP会话中运行

时间:2022-12-30 07:21:34

I have a .net winforms app which has a few animation effects, fade ins and scroll animations etc. These work fine however if I'm in a Remote Desktop Protocol session the animations start to grate.

我有一个.net winforms应用程序,它有一些动画效果,淡入和滚动动画等。但这些工作正常,但如果我在远程桌面协议会话中动画开始grate。

Can someone suggest a way of determining whether or not an app is running across an RDP session so I can turn the effects off in this case?

有人可以建议一种方法来确定应用程序是否在RDP会话中运行,这样我可以在这种情况下关闭效果吗?

4 个解决方案

#1


20  

Assuming you're at least on .NET Framework 2.0, there's no need to use P/Invoke: just check the value of System.Windows.Forms.SystemInformation.TerminalServerSession (MSDN).

假设您至少使用的是.NET Framework 2.0,则无需使用P / Invoke:只需检查System.Windows.Forms.SystemInformation.TerminalServerSession(MSDN)的值即可。

#2


7  

See a similar question i asked: How to check if we’re running on battery?

看到一个类似的问题我问:如何检查我们是否使用电池运行?

Because if you're running on battery you also want to disable animations.

因为如果您使用电池运行,您还需要禁用动画。

/// <summary>
/// Indicates if we're running in a remote desktop session.
/// If we are, then you MUST disable animations and double buffering i.e. Pay your taxes!
/// 
/// </summary>
/// <returns></returns>
public static Boolean IsRemoteSession
{
    //This is just a friendly wrapper around the built-in way
    get    
    {
        return System.Windows.Forms.SystemInformation.TerminalServerSession;    
    }
}

And then to check if you're running on battery:

然后检查你是否使用电池运行:

/// <summary>
/// Indicates if we're running on battery power.
/// If we are, then disable CPU wasting things like animations, background operations, network, I/O, etc
/// </summary>
public static Boolean IsRunningOnBattery
{
   get
   {
      PowerLineStatus pls = System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus;
      if (pls == PowerLineStatus.Offline)
      {
         //Offline means running on battery
         return true;
      }
      else
      {
         return false;
      }
   }
}

Which you can just combine into one:

您可以将其组合成一个:

public Boolean UseAnimations()
{
   return 
      (!System.Windows.Forms.SystemInformation.TerminalServerSession) &&
      (System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus != PowerLineStatus.Offline);
}

Note: This question was already asked (Determine if a program is running on a Remote Desktop)

注意:此问题已被提出(确定程序是否在远程桌面上运行)

#3


3  

In addition to making the initial check to see if your desktop is running in a RDP session, you may also want to handle the situation where the remote session is connected or disconnected while your ap is running. You could have an app running on the console session and then someone could initiate a RDP connection to the console. Unless your application is periodically making the call to GetSystemMetrics, it's going to assume that it's not running as a terminal services session.

除了进行初始检查以查看您的桌面是否在RDP会话中运行之外,您可能还希望处理在ap运行时连接或断开远程会话的情况。您可以在控制台会话上运行应用程序,然后有人可以启动与控制台的RDP连接。除非您的应用程序定期调用GetSystemMetrics,否则它将假定它不作为终端服务会话运行。

You would have your app register for session update notificiations through WTSRegisterSessionNotification. That will allow your applicaiton to be immediately notified is a remote connection has been opened or closed to the desktop session that your application is running under. See here for some sample C# code.

您可以让您的应用通过WTSRegisterSessionNotification注册会话更新通知。这将允许您的应用程序立即得到通知,即已打开或关闭运行您的应用程序的桌面会话的远程连接。请参阅此处获取一些C#代码示例。

For a some good Delphi Win32 exampale code for using WTSRegisterSessionNotification, see this page.

有关使用WTSRegisterSessionNotification的一些好的Delphi Win32 exampale代码,请参阅此页面。

#4


2  

Use the GetSystemMetrics() function in the user32.dll. Use PInvoke to call it. The following is sample code provided by the first link. The second link tells you how to invoke it in .NET.

使用user32.dll中的GetSystemMetrics()函数。使用PInvoke来调用它。以下是第一个链接提供的示例代码。第二个链接告诉您如何在.NET中调用它。

 BOOL IsRemoteSession(void){
      return GetSystemMetrics( SM_REMOTESESSION );
   }

Complete code:

完整代码:

[DllImport("User32.dll")]
static extern Boolean IsRemoteSession()
{
 return GetSystemMetrics ( SM_REMOTESESSION);
}

There's also the SystemInformation.TerminalServerSession Property, which determines whether or not the client is connected to a Terminal Server session. The code provided by MSDN is extensive, so I won't duplicate it here.

还有SystemInformation.TerminalServerSession属性,它确定客户端是否连接到终端服务器会话。 MSDN提供的代码很广泛,所以我不会在这里复制它。

#1


20  

Assuming you're at least on .NET Framework 2.0, there's no need to use P/Invoke: just check the value of System.Windows.Forms.SystemInformation.TerminalServerSession (MSDN).

假设您至少使用的是.NET Framework 2.0,则无需使用P / Invoke:只需检查System.Windows.Forms.SystemInformation.TerminalServerSession(MSDN)的值即可。

#2


7  

See a similar question i asked: How to check if we’re running on battery?

看到一个类似的问题我问:如何检查我们是否使用电池运行?

Because if you're running on battery you also want to disable animations.

因为如果您使用电池运行,您还需要禁用动画。

/// <summary>
/// Indicates if we're running in a remote desktop session.
/// If we are, then you MUST disable animations and double buffering i.e. Pay your taxes!
/// 
/// </summary>
/// <returns></returns>
public static Boolean IsRemoteSession
{
    //This is just a friendly wrapper around the built-in way
    get    
    {
        return System.Windows.Forms.SystemInformation.TerminalServerSession;    
    }
}

And then to check if you're running on battery:

然后检查你是否使用电池运行:

/// <summary>
/// Indicates if we're running on battery power.
/// If we are, then disable CPU wasting things like animations, background operations, network, I/O, etc
/// </summary>
public static Boolean IsRunningOnBattery
{
   get
   {
      PowerLineStatus pls = System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus;
      if (pls == PowerLineStatus.Offline)
      {
         //Offline means running on battery
         return true;
      }
      else
      {
         return false;
      }
   }
}

Which you can just combine into one:

您可以将其组合成一个:

public Boolean UseAnimations()
{
   return 
      (!System.Windows.Forms.SystemInformation.TerminalServerSession) &&
      (System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus != PowerLineStatus.Offline);
}

Note: This question was already asked (Determine if a program is running on a Remote Desktop)

注意:此问题已被提出(确定程序是否在远程桌面上运行)

#3


3  

In addition to making the initial check to see if your desktop is running in a RDP session, you may also want to handle the situation where the remote session is connected or disconnected while your ap is running. You could have an app running on the console session and then someone could initiate a RDP connection to the console. Unless your application is periodically making the call to GetSystemMetrics, it's going to assume that it's not running as a terminal services session.

除了进行初始检查以查看您的桌面是否在RDP会话中运行之外,您可能还希望处理在ap运行时连接或断开远程会话的情况。您可以在控制台会话上运行应用程序,然后有人可以启动与控制台的RDP连接。除非您的应用程序定期调用GetSystemMetrics,否则它将假定它不作为终端服务会话运行。

You would have your app register for session update notificiations through WTSRegisterSessionNotification. That will allow your applicaiton to be immediately notified is a remote connection has been opened or closed to the desktop session that your application is running under. See here for some sample C# code.

您可以让您的应用通过WTSRegisterSessionNotification注册会话更新通知。这将允许您的应用程序立即得到通知,即已打开或关闭运行您的应用程序的桌面会话的远程连接。请参阅此处获取一些C#代码示例。

For a some good Delphi Win32 exampale code for using WTSRegisterSessionNotification, see this page.

有关使用WTSRegisterSessionNotification的一些好的Delphi Win32 exampale代码,请参阅此页面。

#4


2  

Use the GetSystemMetrics() function in the user32.dll. Use PInvoke to call it. The following is sample code provided by the first link. The second link tells you how to invoke it in .NET.

使用user32.dll中的GetSystemMetrics()函数。使用PInvoke来调用它。以下是第一个链接提供的示例代码。第二个链接告诉您如何在.NET中调用它。

 BOOL IsRemoteSession(void){
      return GetSystemMetrics( SM_REMOTESESSION );
   }

Complete code:

完整代码:

[DllImport("User32.dll")]
static extern Boolean IsRemoteSession()
{
 return GetSystemMetrics ( SM_REMOTESESSION);
}

There's also the SystemInformation.TerminalServerSession Property, which determines whether or not the client is connected to a Terminal Server session. The code provided by MSDN is extensive, so I won't duplicate it here.

还有SystemInformation.TerminalServerSession属性,它确定客户端是否连接到终端服务器会话。 MSDN提供的代码很广泛,所以我不会在这里复制它。