Get owner of a process

T

Tim B

Hi

I am writing a program for a terminalserver.
But the program should startet only once
so i write a methode which returns if the process is running:
....
Process[] processes = Process.GetProcessesByName(ProcessName);

if(processes.Length > 1)
{
return true;

}
else
{
return false;
}
....

so now me problem.

I want that every user on the terminalservice can start the program once!

But when sameone starts it a other user can't start it too!

so i must check the user of the process.
who can i get the owner of the process?

thx

greets tim
 
S

Scott Allen

Hi Tim:

What you want to create is a 'named mutex' with the
System.Threading.Mutex class. I believe the documentation even has a
sample demonstrating how to ensure only one instance of an application
is running.

A Mutex is a kernel object and goes into a 'namespace' for the
terminal service session (not to be confused with .Net namespaces -
it's a kernel object namespace which prevents naming conflicts just
like a .NET namespace). This means it prevents multiple instances of
the application from running in the *same* terminal services session.

Just in case you wanted only one instance of the application for the
entire machine, you can put the Mutex in the global namespace by
prefixing the name with 'Global\'.

You'll find this approach to be more robust then searching through the
process list.

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top