Kill a Process

23 views
Skip to first unread message

wrai...@gmail.com

unread,
Jun 24, 2013, 12:13:34 PM6/24/13
to cassi...@googlegroups.com
Greetings.

I recently stumbled across Cassia while trying to find useful code to interact with our RDS servers. I have integrated Cassia and it works very well. I list our available servers and I can:

*List sessions on a server or all servers (users, sessionID, I.P., idle time, etc.)
*List Processes for a user
*Send a user a detailed message (wait for a reply or not)
*Log a session (user) off
*Send all users a message

When I list sessions I color code sessions that have been idle for a certain time and sessions that are disconnected. This tool has been very useful.

I would like to provide the capability to kill a particular process without simply logging the user off. Looking at Cassia help* it appears that I can use the Kill method in the following way:

Kill(handle,processid)

*I actually found this in TerminaServicesProcess.cs:

public void Kill(int exitCode)
        {
            NativeMethodsHelper.TerminateProcess(_server.Handle, _processId, exitCode);
        }

I would appreciate any code samples that any of you might have showing a good way to select a session and provide the server.handle in the proper way. I suppose I could just select the session from my listview control where I show the session info for each session and use it along with the server name to then get the server handle and pass that into the Kill method.

I'd love to see your solution to using the Kill method.

Dan Ports

unread,
Jun 24, 2013, 2:43:58 PM6/24/13
to cassi...@googlegroups.com
Glad to hear Cassia has been useful to you.

No need to call NativeMethodsHelper.TerminateProcess, which is not part of Cassia's public API; just use ITerminalServicesProcess.Kill -- for example: new TerminalServicesManager().GetLocalServer().GetProcess(someProcessId).Kill().

Dan


--
You received this message because you are subscribed to the Google Groups "Cassia Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cassia-dev+...@googlegroups.com.
To post to this group, send email to cassi...@googlegroups.com.
Visit this group at http://groups.google.com/group/cassia-dev.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

wrai...@gmail.com

unread,
Jun 25, 2013, 2:19:59 PM6/25/13
to cassi...@googlegroups.com
Thanks Dan.

wrai...@gmail.com

unread,
Jun 28, 2013, 11:26:01 AM6/28/13
to cassi...@googlegroups.com
OK, I have had an opportunity to try out the Kill() method. I have had no luck executing this on a remote server. I have used the following code:

        private void killSelectedProcessToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                TerminalServicesManager tsManager = new TerminalServicesManager();
                ITerminalServer server = tsManager.GetRemoteServer("RALIT3TS09");
                server.Open();
                bool isServerOpen = server.IsOpen;
                tsManager.GetRemoteServer(server.ToString()).GetProcess(5160).Kill();
 
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + 
                    Environment.NewLine + "==============================================" +
                    Environment.NewLine + ex.Message + 
                    Environment.NewLine + "==============================================" + 
                    Environment.NewLine + ex.InnerException);
            }
        }//End of killSelectedProcessToolStripMenuItem_Click

I added the testing variable bool isServerOpen simply to evaluate any weird happenings. I attached two screenshot files showing the code and showing that the connection is in fact open at the time of the call to Kill(). The second attachment simply shows the exception that I displayed through the messagebox.

As you can see by viewing serverOpen.jpg, the server connection hosting the session and process is in fact open yet the exception indicates that it is not. The processID of  5160 is correct for the running process that I want to kill and it is running at the time of calling the Kill() method.

Any ideas would be welcome.
serverOpen.jpg
Exception.jpg

Dan Ports

unread,
Jul 5, 2013, 2:49:41 PM7/5/13
to cassi...@googlegroups.com
It looks like you've got a bug in your code here: in the first few lines of the try block, the code creates and opens a connection to the server, but then in the highlighted line where the debugger is stopped, the code creates a second, independent server object and then immediately calls GetProcess() and Kill() without opening the connection. What you'd want to do is replace that highlighted line with server.GetProcess(5160).Kill(); and you should be all set. I'd also recommend wrapping the code that uses the ITerminalServer object in a using block to dispose of the connection promptly.

Hope that helps!

Dan

Wraith

unread,
Jul 5, 2013, 5:16:41 PM7/5/13
to cassi...@googlegroups.com
Thanks very much for the code review. I'll correct my mistakes. 

Sent from my iPhone
You received this message because you are subscribed to a topic in the Google Groups "Cassia Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cassia-dev/v7bIgDN79do/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cassia-dev+...@googlegroups.com.

wrai...@gmail.com

unread,
Jul 8, 2013, 1:41:07 PM7/8/13
to cassi...@googlegroups.com
I corrected the code and it works as it should. Thank you! 

To unsubscribe from this group and stop receiving emails from it, send an email to cassia-dev+unsubscribe@googlegroups.com.

To post to this group, send email to cassi...@googlegroups.com.
Visit this group at http://groups.google.com/group/cassia-dev.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to a topic in the Google Groups "Cassia Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cassia-dev/v7bIgDN79do/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cassia-dev+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages