tags:

views:

1318

answers:

4

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?

+1  A: 

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.

George Stocker
+15  A: 

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

Arnout
+3  A: 

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.

Chris Miller
+2  A: 

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)

Ian Boyd