0

I run a Citrix session for Enterprise Architect application.

I make some modifications that requires close of the Citrix session.

When I close the Citrix window, it just seems to close, but still running at the background.(I can see it in the lost of running programs. )

How can I force the Citrix session to be closed, using C#?

user3165438
  • 2,631
  • 7
  • 34
  • 54
  • Do you have the possibility of detecting that the session has ended? If so you could possible simply kill the corresponding process. – Markus Safar Oct 29 '14 at 11:29
  • @MarkusSafar, Thanks. You're right. But I would like to run a process whenever a user closes the Citrix window, that kills it totally. Any ideas? – user3165438 Oct 29 '14 at 11:31
  • Actually that was my question :D I'm pretty sure there are some tricky Windows-API calls you could use for monitoring state changes of specific windows but it would be easier if you had some other trigger like every time a session ends a specific file is updated or something like that. Does closing the session mean that the user logs off? – Markus Safar Oct 29 '14 at 11:34
  • @MarkusSafar, No, closing the session means closing the program that runs on a server. This does not mean logging of Citrix. Can you please guide me further about how to close the session? – user3165438 Oct 29 '14 at 11:37
  • Hm... I guess the server where the application is running is no windows machine, right? Otherwise you could use the `Process` class to evaluate whether the process (your application which is being closed on closing the citrix session) is still running or not. If not you can kill the process. – Markus Safar Oct 29 '14 at 11:43
  • @MarkusSafar, Well, the application that is running via Citrix session is closed indeed. I can see it closed and an empty window (citrix itself) is still running. What is the next step? – user3165438 Oct 29 '14 at 11:53
  • Next step would be to write an application that is able to monitor the processes on this remote machine. Is this remote machine a windows machine? – Markus Safar Oct 29 '14 at 11:55
  • 2
    Possibly this could help too [Detect termination of Citrix session](http://stackoverflow.com/a/10703354/2921691) – Markus Safar Oct 29 '14 at 12:01
  • @MarkusSafar, The Citrix server is a Windows R2 2008 edition. – user3165438 Oct 29 '14 at 12:03
  • Great, just use something like the following code to monitor the processes running on the server (the user which run's the application must have the permission to do this on the server of course). However I would prefer the way with the detecting the session change. `public static bool IsProcessRunning(string processName, string serverName) { return Process.GetProcessesByName(processName, serverName).Any(); }` – Markus Safar Oct 29 '14 at 12:13
  • @MarkusSafar, I'll try and update. When I success I'll notify yu so that you could post it as an answer and I'll accept it. Thanks. – user3165438 Oct 29 '14 at 12:23
  • Ok, much success ;-) – Markus Safar Oct 29 '14 at 12:25

1 Answers1

1

So I've read this a couple of times and I'm still not 100% sure whether you're running your C# on the client that runs Citrix Receiver on inside the session on the server - so I'll answer both ways.

1) How-to close the session from the client

You can use the Live Monitoring API accessed via ICO. ICO is the ICA Client Object, a COM based API to control the win32 Citrix Receiver. The following PowerShell sample shows you how to enumerate sessions on the client and log them off. The code to do this in C# will be very similar. This sample is matching on the name of the server, but you can use something different. E.g. logoff all sessions, or matched based on the name of the published app.

http://smulpuru.wordpress.com/2013/06/27/client-side-xenapp-session-enumeration-and-selective-logoff-based-on-xenapp-server-using-ica-client-object-wficalib/

NOTE: you must set the two registry keys mentioned at the top of the PowerShell sample otherwise the Live Monitoring API won't work (due to security reasons).

2) How-to close the session from the server

This is much easier, all you need to do is simply trigger a logoff - there are many ways to do this. One of the more common ways is to use WTSLogoffSession. You'll need to pinvoke from C#, but the definition is pretty simple:

    [DllImport("wtsapi32.dll", SetLastError = true)]
    public static extern bool WTSLogoffSession(IntPtr hServer, int sessionId, bool bWait);
donovan
  • 1,442
  • 9
  • 18