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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting currently running application.

Status
Not open for further replies.

chronobyte

Programmer
Mar 30, 2002
5
0
0
US
Hello, I was wondering if anybody can help me with this:
I'd like to create an application that is a singleton. If a user try to run another instance while one is still running it will just activate the existing one.
How can I accomplish this?
Is there a way to detect if the same application is already running?

Any help would be appreciated. Thanks!
 
Here is part of your answer:

// this find all the process that have the same
// name as the current process.
Process[] processList = Process.GetProcessesByName( Process.GetCurrentProcess().ProcessName );

if( processList.Length > 1 )
{
// Bring other process to the forground and exit
}
 
Just thought I would add a little more:

// Declare this inside the class

[DllImportAttribute( "User32.dll" )]
static extern int FindWindow( String ClassName, String WindowName );
[DllImportAttribute( "User32.dll" )]
static extern int SetForegroundWindow( int hWnd );

// Then do something like this inside you code
int hWnd = FindWindow( null, "Untitled - NotePad" );
if( hWnd > 0 )
{
SetForegroundWindow( hWnd );
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top