How to check if only 1 instance of an application is running?
-
Hello gurus, I'd like to know how can see if my application already runs or not? Only 1 instance of my application can run. How to do that? Best regards. Fred.
There is no spoon.
Hi You could use this method: Use the following code on you load or main method
if (PrevInstance()) { MessageBox.Show("The program is already running"); Application.Exit(); }
This is the method called from your main codeprivate static bool PrevInstance() { if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1) { return true; } return false; }
Hope this helps. -
Hi You could use this method: Use the following code on you load or main method
if (PrevInstance()) { MessageBox.Show("The program is already running"); Application.Exit(); }
This is the method called from your main codeprivate static bool PrevInstance() { if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1) { return true; } return false; }
Hope this helps. -
Hi You could use this method: Use the following code on you load or main method
if (PrevInstance()) { MessageBox.Show("The program is already running"); Application.Exit(); }
This is the method called from your main codeprivate static bool PrevInstance() { if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1) { return true; } return false; }
Hope this helps. -
ok, it works. And how can I Activate the main window of the already running application??? Fred.
There is no spoon.
Hello, You have to get the "MainWindowHandle" property of the process, and use it in the "ShowWindow" method of "user32.dll". Here is a article where it's discriped: http://www.codeproject.com/useritems/MultipleInstance.asp[^] All the best, Martin
-
Hello, You have to get the "MainWindowHandle" property of the process, and use it in the "ShowWindow" method of "user32.dll". Here is a article where it's discriped: http://www.codeproject.com/useritems/MultipleInstance.asp[^] All the best, Martin