blocking 2 of the same exe's from runing  
Author Message
Smacker





PostPosted: Visual C# General, blocking 2 of the same exe's from runing Top

im building a client/server application and I only want to allow the user to run one client per computer, what are some ways to block that 2nd client from starting up on a comp that is already runing a client

thanks



Visual C#4  
 
 
Brendan Grant





PostPosted: Visual C# General, blocking 2 of the same exe's from runing Top

One of the best ways to do this is for your application to define a mutex that identifies your application to other copies of itself. Below is the code I run for this task:

System.Threading.Mutex singleInstance;
private bool AmIAlreadyRunning()
{
singleInstance = new System.Threading.Mutex(false,"MyApplicationNameMutex");
return !singleInstance.WaitOne(1,true);
}

With this I simply call AmIAlreadyRunning() to find out the answer to that question, if true I simply exit the application, if the instance of the app knows that it is the only one and can proceed on it’s own.

Does this work for you

Also, be sure to change MyApplicationNameMutex to a name of your choosing (perhaps based on your application & version name).



 
 
Xancholy





PostPosted: Visual C# General, blocking 2 of the same exe's from runing Top

Brendan, nice code.

What if an instance is already running minimized. How would I maximize the running instance and then exit the new instance Can you show me some code to do that



 
 
GavH





PostPosted: Visual C# General, blocking 2 of the same exe's from runing Top

You need to use win32 interop,

[DllImport("user32.dll")]public static extern int FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]public static extern int SendMessage(int hWnd, int msg, int wParam, int lParam);

If you only need to access the main application window, those two methods will surfice, if you need access to child window you will also need :

[DllImport("user32.dll")]public static extern bool EnumChildWindows(int hWnd, Delegate lpEnumFunc, int lParam);

Which works with a call back (lpEnumFunc) and passes the Hwnd of each child of the Hwnd past into it, one by one.

To maximise the main window of an application you first need to obtain its handle, then you need to send it the maximise message, an untested example follows:

//you need to have the import methods and these two constants available
const int WM_SYSCOMMAND =
0x0112;
const int SC_MAXIMIZE =
0xF030

//get the window handle
int hWnd = FindWindow("The Class ie MyApplication","The name or blank for topmost");

if(hWnd > -1)//if we have a handle, send it the maximize message
SendMessage(hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);

Regards





 
 
Xancholy





PostPosted: Visual C# General, blocking 2 of the same exe's from runing Top

Thanks for that pointer. I also found this code under heading:

48.5 How do I maximize my main window




 
 
GavH





PostPosted: Visual C# General, blocking 2 of the same exe's from runing Top

Thats a useful block of examples.

The example in question however only informs you how to maximise the main window of a process you have started, not one that is already running, that you do not have the hWnd for.

Regards