If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Check for a process per user VB

I posted this on several vb forums with no avail so i figure i would post here because i have always gotten better help here than anywhere else.

I have an application on a terminal server that runs for each user as upon login and is intended to always be running for them. The problem i found out was that if they didnt logout and just disconnected then reconnected, another instance of this app would run as well as the currently running one, so on and so forth. I then added this simple code:

Code:

Dim Proc As System.Diagnostics.Process
If proc.Length > 1 And Environment.UserName = Employee.NTUserName Then
MsgBox("Another instance of TimeClock is already running. Terminating.")
End
End If

That does what i want but it doesnt care about multiple users using the app. It looks and if more than one instance is running for all users then it will end so only one user can use it at any time. I need to check if more than one instance is running per user.

Another thing i tried is this:

Code:

Dim processList As System.Diagnostics.Process()
Dim thisProc As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess
Dim thisSessionID As Long = thisProc.SessionId
processList = System.Diagnostics.Process.GetProcesses()
Dim Proc As System.Diagnostics.Process
Dim count As Integer = 0
For Each Proc In processList
If Proc.SessionId = thisSessionID And Proc.ProcessName = "LSTimeClock.exe" Then
count += 1
End If
If count > 1 Then
MsgBox("There is another instance of LSTimeClock Running")
End
End If

This code looks like it should do what i want but it doesnt. I fairly recently got thrown into vb programming and for terminal services for that matter so there are lots of extra things i didnt expect like this.

Any help would rock.

Thx
Mung

I believe in making the world safe for our children, but not our childrenís children, because I donít think children should be having sex. -- Jack Handey

SirDice, can a service have a system tray icon for every user to interact with? If so, then i would definitely do that.

Aard and Wolfe, It sound pretty easy to do what you suggest, just have some file created by the app and do a check for that each time. I will probably do that for a quick fix but I really think that there has to be a way to see all of the processes for any specific user. If task manager can show users and their processes there has to be some code in the .NET framework that can do the same.

Either way, thanks for the ideas and if anyone happens to know the fix im looking for, i would love to know.
Thx

I believe in making the world safe for our children, but not our childrenís children, because I donít think children should be having sex. -- Jack Handey

I would also expect your little script to work - what is the reason it does not work?

Although the approach itself is not exactly the most elegant way (check
SirDice's remarks!), it is an interesting issue just by itself. Thus:

There are other ways to enumerate the running processes supplemented with
the SessionId and the Owner. Try them, maybe one will work for you.

1. WMI

Add a reference to windows.management and try the following
(example for illustrative purposes - you will figure out the rest)

Code:

Dim ms As New ManagementObjectSearcher("SELECT * FROM Win32_Process")
For Each mo As ManagementObject In ms.Get
If CUInt(mo.InvokeMethod("GetOwner", args)) = 0 Then
Dim objExecutablePath As Object = mo.Properties("ExecutablePath").Value
If Not objExecutablePath Is Nothing Then
Console.WriteLine((args(1).ToString & "\" & args(0).ToString))
Console.WriteLine(objExecutablePath.ToString)
Console.WriteLine(mo.Properties("SessionId").Value.ToString)
Console.WriteLine()
End If
End If
Next

Aw sweet sec, thats exactly what i was looking for. A coworker said he remembers a way to query for processes but couldnt remember what it was called and i didnt even think he was right. Im gonna go ahead and figure out the code, looks pretty straight forward. Hopefully i can get it to work... Never thought i would be doing windows programming at a web design company but meh, whaddya gonna do?

Originally Posted by sec_ware

I would also expect your little script to work - what is the reason it does not work?

Well kinda funny, I thought it was working because nobody had said anything in the whole week i was live tesing it but that was because only one person was using it and the beginning of the next week i get a call saying hey, only one person can use this.

all it was doing was looking to see if more than one of that process was running. didnt matter which user was running it but it would kill itself if more than one instance was running.

Thanks all for your help!

Last edited by mungyun; July 18th, 2007 at 06:54 PM.

I believe in making the world safe for our children, but not our childrenís children, because I donít think children should be having sex. -- Jack Handey