Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

Terminal Server Awareness


:P
On this page:

 

Got a note a few days ago from a customer who’s running my West Wind Web Monitor tool for site monitoring with a small but annoying problem. West Wind Web Monitor is a server monitoring utility so it’s quite common to run it on a remote server and then terminal into the machine to access the application remotely. Although West Wind Web Monitor has an ASP.NET Web Interface it actually turns out that most people don’t install that and opt to running the GUI application over Terminal Services.

 

West Wind Web Monitor uses toolbars and a splash screen that well, has a nice fade effect which is a disaster in Terminal Services mode. As the image is faded under TS you get horrible stutter and the image takes 30 second or so to go away – far from optimal. This has actually bitten me too on a few occasions and I meant to fix it for a while.


Anyway the solution to this is pretty straight forward. You can check whether your app isrunning under Terminal Server with the following code:

 

public class WebMonitorUtilities

{

    [DllImport("user32.dll")]

    public static extern int GetSystemMetrics(int nIndex);

 

    private const int SM_REMOTESESSION = 0x1000;

 

    /// <summary>

    /// Determines whether Terminal Server is running.

    /// Requires full trust to check.

    /// </summary>

    public static bool IsTerminalServer =  ( 0 != GetSystemMetrics(SM_REMOTESESSION) );

 

}

 

Now in my code I can simply use this function and conditionally check for Terminal Server. For example, here’s the fade out code for the splash screen (the form’s running on a separate thread):

 

void FadeOut()

{

    if (WebMonitorUtilities.IsTerminalServer || this.InFadeOut)

        return;

 

    this.InFadeOut = true;

 

      for (double x=0.98; x >= 0; x -= .05)

      {

            this.Opacity = x;

            Application.DoEvents();

            this.Refresh();

            System.Threading.Thread.Sleep(50);

      }

 

    this.InFadeOut = false;

}

 

A few other things that I use the check for is the toolbar – if Terminal Server is running I use a simple toolbar color scheme rather than than a themed interface which will look better in low res and low color scenarios as well as run a little more switftly.

 

This is Interop code so it requires high trust unfortunately, but I don't think there's a way to do this without the Win32 code. Not a problem in this application since it requires high permissions anyway (and typically runs as a service), but might be an issue in other applications.

 

Slow time of the year - Fix up days

Since this is the ‘slow’ time of the year, I’ve been spending a bit more time with some of my tools and updating them. The last few months I’ve been buried in consulting and writing work that I had barely enough time to attend to my various products besides support. That will change hopefully starting now with my schedule freed up a bit for more internal work and getting on with putting some of those piled up ideas integrated <s>…

 

 For West Wind Web Monitor I actually spend the last couple days fixing a number of outstanding issues from my bug list as well as updating West Wind Web Monitor to .NET 2.0. West Wind Web Monitor is the last of my tool apps that was left running under 1.1 and I’m glad it’s moved over to 2.0. As a result the upgrade results in a faster starting app that has a smaller memory footprint (some but not nearly as much as I would have liked <s>).

 

It’s been a while since I’ve updated an existing app but it was a pretty painless move to get this done although there were a few gothas regarding the thread management that I had to deal with. One thing that worked in my favor here is that I used a third party toolbar control (Sandbar) which spared me the pain of updating 1.x toolbars to 2.x. It also allows for easy switching of the renderer so doing the Terminal Server switching is easy – something the stock toolbar doesn’t do well. Of course I had to do the toolbar translation some time ago, which was not a pleasant exercise even for this moderately busy toolbar interface <shrug>.

 

Apparently .NET 2.0 is a lot more strict with a number of UI related threading issues and I ended up updating the UI udpate layer to be almost completely ansynchronous since most UI updates are fired from non-UI threads. There have also been a few updates to make sure West Wind Web Monitor runs properly under Vista which is also addressed.

 

Overall, this was a productive update – a few new features and moving up to .NET 2.0 – the 2.0 update also improves overall performance and startup performance as well as using a smaller footprint than the 1.x version did! Cool… It feels like I got something done at the end of the day – there haven’t been many days like this recently <s>…

Posted in .NET  

The Voices of Reason


 

David M. Kean
December 31, 2006

# re: Terminal Server Awareness

Or you could simply look at SystemInformation.TerminalServerSession...

Rick Strahl
January 01, 2007

# re: Terminal Server Awareness

Thanks David! Much easier that and without any Interop security issues.

TopXML Reblogger XML News
February 22, 2007

# TopXML : The Daily Grind 1045, in BizTalk Orchestration

The Daily Grind 1045, in BizTalk Orchestration

D. Andrews
March 27, 2007

# re: Terminal Server Awareness

I am testing an application that uses this method to determine if it's running under RDP. I have a full matrix of operating systems. Ideally I could test both modes on "headless" machines. Is there a way to spoof this so that a program launched in an RDP session wouldn't know it was remote?

Having to attach a keyboard and monitor to each machine in turn will be a giant pain, as you can imagine.

Rick Strahl's Web Log
June 23, 2007

# Rick Strahl's Web Log


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024