Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to prevent user to start the same app more than once? 3

Status
Not open for further replies.

hh2424

Programmer
Feb 18, 2003
37
US
How can I prevent users to start the application more than once using C# ?

thanks,
 
Code:
static void Main()
{
    System.Diagnostics.Process pThis = System.Diagnostics.Process.GetCurrentProcess();

    foreach(System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
    {
        if(p.Id != pThis.Id && p.ProcessName == pThis.ProcessName)
        {
            MessageBox.Show("Already Running!!!");

            return;
        }
    }

    Application.Run(new Form1());
}
 
Thank you for your reply.

But this doesn't work with mulitple users (i.e. Terminal Server.) I'm trying to retrieve the process owner and compare it with the logged in user. Does anyone know how I can do that?

thanks,
 
The System.Diagnostics.Process class used in the SHelton example gives you access to the processes running on the same machine not on the different machines.
The above example is enough to detect multiple instances on the SAME process running on the SAME machine.
An object of System.Diagnostics.Process class gives you all you need about a process.
Here is another way to detect multiple instances using Mutex object:

public class MainForm : System.Windows.Forms.Form
{
private static Mutex _AppMutex;
// ...
}

[STAThread]
static void Main()
{
_AppMutex = new Mutex( true, "My application" );
if( _AppMutex.WaitOne( 0, false ) )
{
Application.Run( new MainForm() );
}
else
{
System.Windows.Forms.MessageBox.Show( "Another instance of My application is already running.");
}
}

For the Terminal Server you should implement something to manage that on the machine where resides your exe.
A potential solution:
- Let be MyApp.exe on a machine (server)
- Create a service named for example: MyApp_Service.exe and install it on the server with MyApp.exe
- Create a simple application for example: MyApp_Client.exe and it will do:
-identify the user
-connect to the MyApp_Service.exe
-send a request (tcp.Send()) to start the MyApp.exe. The MyApp_Service.exe read the request and is looking on the server how many instances of the MyApp.exe are running and takes actions.
-When MyApp_Service.exe starts the MyApp.exe it passes a keyword to the MyApp.exe
- MyApp.exe will check this kwyword first. If it is not recognized then the MyApp.exe exits.
That means you never start directly MyApp.exe.
Only MyApp_Service.exe is doing that upon an identified request.
If someone clicks on the MyApp.exe on the server that will not work because the keyword, so you can manage number of instances.

There are also other solutions, for example using a centalized and safe logging file.

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top