views:

214

answers:

1

I have a short program that is used exclusively with a remote desktop connection that's been set to only run that program and not allow any other access into the remote machine. Previously, the program just exited and let the connection terminate, but it was very slow, so I wrote the following code to terminate the remote session when the program is done running.

    [DllImport("wtsapi32.dll", SetLastError = true)]
    static extern bool WTSLogoffSession(IntPtr hServer, int SessionId, bool bWait);

    private IntPtr WTS_CURRENT_SERVER_HANDLE;
    private const int WTS_CURRENT_SESSION = -1;

    ...

    private void HardTerminalExit()
    {
        WTSLogoffSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, false);
    }

This works fine when this program is in its production environment, used by the people who are remoting in using a specific RDP connection file. The connection exits after the program is run. However, when testing and debugging this program, my computer restarts after every run.

I'm looking for a good way to distinguish between these cases. Should I set up some kind of debug script that remotes in and runs the program remotely? Or is there some way to programmatically detect whether the program is running in debug mode and just disable the exit procedure in that case?

+2  A: 

You can use the pragma directives:

private void HardTerminalExit()
{
  #if DEBUG
  // Soft Exit
  #else
  // Hard Exit
  #end if
}

I believe it's better practice to use the newer ConditionalAttribute, but I don't think you can negate that, so you'd need to set an environment variable for "RELEASE" yourself:

[Conditional ( "RELEASE" )]
private void HardTerminalExit()
{
  // Hard Exit, only called when "RELEASE" is defined.
}

This will tell the compiler to only call this method when the "RELEASE" environment variable is declared somewhere - this can declared in during compilation (Conditional compilation symbols on the Build property pane) or as part of the operating system shell, see ConditionalAttribute for more inforamation on this.

Zhaph - Ben Duguid
Thanks, I will try the second option
Tony Peterson