Hi All,
As a noob programmer, I have been tasked with developing a solution
which allows a program (be it console / form) to do a graceful logout
on a RDP session that has been disconnected. A little background:- we
have a database application running on 6 terminal servers where users
log into to access the application. Some users at the end of the day
will correctly logout of the terminal server by clicking Start ---->
LogOff.
Others will disconnect the session thus leaving the application
running which causes problems as their data can not be compacted on a
night when maintenance runs. I simply can not reset / logoff the
session through the Terminal Services Manager as it is not graceful
and also damages the datasets.
I have used Cassia so far to develop a solution which achieves a
graceful logout if the session is disconnected for over 5 minutes.
However, it only works on the first user that logged in to the server
out of the many hundreds that login.
Could someone please take a look at the code below and see where I am
going wrong. I need to extract the current session per user per
application running.
I think it is something to do with the foreach loop on
ITerminalServicesSession
Thanks,
<code>
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.DirectoryServices.AccountManagement;
using Cassia;
//
************************************************************************************************************************************//
//-------------------------------------------------------******CASSIA
DLL USED******--------------------------------------------------//
//
http://code.google.com/p/
cassia// //
//
http://www.opensource.org/licenses/mit-
license.php// //
//Open Source Initiative OSI - The MIT License
(MIT):Licensing //
//
************************************************************************************************************************************// //
//The MIT License
(MIT) //
//Copyright (c) <year> <copyright
holders> //
//
************************************************************************************************************************************//
//Permission is hereby granted, free of charge, to any person
obtaining a copy of this software
and //
//associated documentation files (the "Software"), to deal in the
Software without restriction, //
//including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, //
//and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, //
//subject to the following
conditions: //
//
************************************************************************************************************************************//
//The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the
Software. //
//
************************************************************************************************************************************//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, //
//INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, //
//FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. //
//IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, //
//WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE //
//USE OR OTHER DEALINGS IN THE
SOFTWARE. //
//
************************************************************************************************************************************//
namespace Windows_Information
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
string sessionStatus = "";
Boolean keepLooping = true;
//System.Diagnostics.Process.;
public Form1()
{
InitializeComponent();
}
private void itsAllaboutTime(object sender, EventArgs e)
{
{
ITerminalServicesManager manager = new
TerminalServicesManager();
using (ITerminalServer server =
manager.GetLocalServer())
{
server.Open();
foreach (ITerminalServicesSession session in
server.GetSessions())
{
sessionStatus =
session.ConnectionState.ToString();
}
}
}
int systemSincelastEntry = Environment.TickCount;
int LastInputTicks = 0;
int idleTime = 0;
LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();
LastInputInfo.cbSize =
(uint)Marshal.SizeOf(LastInputInfo);
LastInputInfo.dwTime = 0;
if (GetLastInputInfo(ref LastInputInfo))
{
LastInputTicks = (int)LastInputInfo.dwTime;
idleTime = systemSincelastEntry - LastInputTicks;
}
textBox3.Text = Convert.ToString(idleTime) + "
Millisecconds";
textBox4.Text = (sessionStatus);
while (keepLooping)
{
Application.DoEvents();
//System.Threading.Thread.Sleep(300001);
if (sessionStatus == "Active")
{
System.Threading.Thread.Sleep(300001);
keepLooping = true;
}
else if ((sessionStatus == "Disconnected") &&
(idleTime < 299999))
{
keepLooping = true;
}
else if ((sessionStatus == "Disconnected") &&
(idleTime >= 300000))
{
keepLooping = false;
}
}
//string strCmdText;
//strCmdText = "/C shutdown -l";
System.Diagnostics.Process.Start("shutdown", "/l");
Application.Exit();
}
}
}
</code>