Solved

How do I kill a process by user on a Terminal Server

Posted on 2007-04-04
6
322 Views
Last Modified: 2013-11-07
I have a program that needs to kill any previous instances of itself whenever it is opened.
I'm using the following code...

    Dim processes As Process
    Dim temp As Byte, x As Byte
    Dim tempProcess(5) As Process : Dim TempProcessTime As DateTime
    For Each processes In Process.GetProcesses
      If processes.ToString = "System.Diagnostics.Process (SipcaEMR)" Then temp = temp + 1 : tempProcess(temp) = processes : If DateDiff(DateInterval.Second, processes.StartTime, TempProcessTime) < 0 Then TempProcessTime = processes.StartTime
    Next
    If temp > 1 Then
      For x = 1 To temp
        If Not tempProcess(x).StartTime = TempProcessTime Then tempProcess(x).Kill()
      Next
    End If
This works fine on my Fat Clients.
The problem is that this will not work on a terminal server because it will kill of the processes of all the users using this application. I want it to only kill off and previously opened SipcaEMR that this user is running. Leaving the rest alone
0
Comment
Question by:jamescorwin
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:tpwells
ID: 18881959
Add System.Managment as a Reference to your project and then try this function:

    Private Sub KillProcess(ByVal ProcessName As String, ByVal UserName As String)
        Dim iProc As New Management.ManagementClass("Win32_Process")
        Dim procs As Management.ManagementObjectCollection = iProc.GetInstances()
        For Each oProc As Management.ManagementObject In procs
            Dim user(1) As String
            oProc.InvokeMethod("GetOwner", CType(user, Object()))
            If Not user(1) Is Nothing Then
                If (oProc.GetPropertyValue("Caption").ToString() = ProcessName _
                And UserName = user(0).ToString()) Then
                    oProc.InvokeMethod("Terminate", Nothing)
                End If
            End If
        Next
    End Sub

Created From Code on this Post:
http://www.experts-exchange.com/Programming/Misc/Q_21685488.html
0
 

Author Comment

by:jamescorwin
ID: 18882462
When I try to add import System.Management to my list of imports it does not know Management.
I can't seem to find the dll reference for system.management in case I need to browse for that in my references.
I'm am using Visual Studio.Net 2003. I'm in the VB.Net part of it.
How do I add the reference for System.Managment
0
 
LVL 10

Expert Comment

by:tpwells
ID: 18882502
In the Solutions Explorer right-click on The References folder and select "Add Reference". In the dialogue that comes up scroll down the list on the .NET tab until you find the System.Management Component then double-click it and then Click "OK"
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:jamescorwin
ID: 18882552
Nevermind, I found it in Project, Add Reference
0
 

Author Comment

by:jamescorwin
ID: 18883181
Is there a way I can know which instance is the last opened as I do not want to kill that one.
Such as can I use as command like oProc.InvokeMethod("GetOwner", CType(user, Object()))
to get the time it was opened or the amount of time it has been running.
0
 
LVL 10

Accepted Solution

by:
tpwells earned 500 total points
ID: 18883594
You can use the CreationDate Property of the oProc above. Here is some modified code of what I posted above to kill all but the newest process.

    Private Sub KillProcess(ByVal ProcessName As String, ByVal UserName As String)
        Dim Processes As New ArrayList 'list of the processes with this name and user
        Dim iProc As New Management.ManagementClass("Win32_Process")
        Dim procs As Management.ManagementObjectCollection = iProc.GetInstances()
        For Each oProc As Management.ManagementObject In procs
            Dim user(1) As String
            oProc.InvokeMethod("GetOwner", CType(user, Object()))
            If Not user(1) Is Nothing Then
                If (oProc.GetPropertyValue("Caption").ToString() = ProcessName _
                And UserName = user(0).ToString()) Then
                    Dim strCreateTime As Int64 = Convert.ToInt64(oProc.GetPropertyValue("CreationDate").ToString.Split(".".ToCharArray())(0))
                    Dim intHandle As Integer = Convert.ToInt32(oProc.GetPropertyValue("Handle"))
                    Processes.Add(New clsProcess(intHandle, strCreateTime))
                End If
            End If
        Next
        If Processes.Count > 1 Then
            'kill the older processes
            Dim CurrentWinner As clsProcess = CType(Processes(0), clsProcess)
            For i As Integer = 1 To Processes.Count - 1
                CurrentWinner = CheckProcesses(CurrentWinner, CType(Processes(i), clsProcess))
            Next
        End If
    End Sub

    'returns the process that is newer and kills the older one
    Private Function CheckProcesses(ByVal Proc1 As clsProcess, ByVal Proc2 As clsProcess) As clsProcess
        If (Proc1.CreateTime > Proc2.CreateTime) Then
            Proc2.Kill()
            Return Proc1
        Else
            If Not (Proc1 Is Proc2) Then
                Proc1.Kill()
                Return Proc2
            End If
        End If
    End Function

    Private Class clsProcess
        Public Sub New(ByVal ProcHandle As Integer, ByVal ProcCreateTime As Int64)
            _Handle = ProcHandle
            _CreateTime = ProcCreateTime
        End Sub
        Private _Handle As Integer
        Private _CreateTime As Int64
        Private _Killed As Boolean
        Public ReadOnly Property Handle() As Integer
            Get
                Return _Handle
            End Get
        End Property

        Public ReadOnly Property CreateTime() As Int64
            Get
                Return _CreateTime
            End Get
        End Property

        Public ReadOnly Property Killed() As Boolean
            Get
                Return _Killed
            End Get
        End Property

        Public Sub Kill()
            Dim proc As Process = Process.GetProcessById(Handle)
            proc.Kill()
            _Killed = True
        End Sub
    End Class
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question