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

发布于 2024-07-08 07:56:57 字数 145 浏览 10 评论 0原文

我有一个 .net winforms 应用程序,它有一些动画效果、淡入和滚动动画等。这些效果很好,但是如果我处于远程桌面协议会话中,动画就会开始变得刺耳。

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风夜微凉 2024-07-15 07:56:57

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

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

妄司 2024-07-15 07:56:57

请参阅我提出的类似问题:如何检查我们是否'正在使用电池运行?

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

/// <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;    
    }
}

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

/// <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;
      }
   }
}

您可以将其合并为一个:

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

注意:这个问题已经被问过(确定程序是否正在远程桌面上运行)

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)

风筝在阴天搁浅。 2024-07-15 07:56:57

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

您可以让您的应用程序通过 WTSRegisterSessionNotification 注册会话更新通知。 如果您的应用程序正在运行的桌面会话的远程连接已打开或关闭,那么您的应用程序将立即收到通知。 有关一些示例 C#,请参阅此处代码。

有关使用 WTSRegisterSessionNotification 的一些优秀的 Delphi Win32 示例代码,请参阅此页面

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.

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.

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

笑叹一世浮沉 2024-07-15 07:56:57

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

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

完整代码:

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

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

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.

 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文