18

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

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

4

4 回答 4

20

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

于 2008-11-17T12:29:08.733 回答
7

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

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

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

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

于 2008-12-16T18:09:48.350 回答
3

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

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

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

于 2008-12-16T17:59:56.823 回答
2

使用 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属性,它确定客户端是否连接到终端服务器会话。MSDN 提供的代码比较丰富,这里不再赘述。

于 2008-11-17T12:06:53.007 回答