Determine if only one instance is running: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added C++ on Windows. I'd be interested in seeing a POSIX solution.)
Line 3: Line 3:
This task is to determine if there is only one instance of an application running.
This task is to determine if there is only one instance of an application running.


=={{header|Visual Basic}}==
=={{header|C++}}==
===Microsoft Windows===
'''Language Version:''' 6
'''Operating System:''' Windows 2000 or later.


This line needs to be near the top of the file (or in stdafx.h, if you use one.)
Dim onlyInstance as Boolean
#include <afx.h>
onlyInstance = not App.PrevInstance

You need a variable of type HANDLE with the same lifetime as your program. Perhaps as a member of your CWinApp object.
HANDLE mutex;

At the earliest possible point in your program, you need to initialize it and perform your check. "MyApp" should be a string unique to your application. See [http://msdn2.microsoft.com/en-us/library/ms682411.aspx here] for full details.

mutex = CreateMutex( NULL, TRUE, "MyApp" );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
// There's another instance running. What do you do?
}

Finally, near the end of your program, you need to close the mutex.
CloseHandle( mutex );


=={{header|Python}}==
=={{header|Python}}==
Line 43: Line 58:
return 1
return 1
</pre>
</pre>

=={{header|Visual Basic}}==
'''Language Version:''' 6

Dim onlyInstance as Boolean
onlyInstance = not App.PrevInstance

Revision as of 06:30, 9 December 2007

Task
Determine if only one instance is running
You are encouraged to solve this task according to the task description, using any language you may know.

This task is to determine if there is only one instance of an application running.

C++

Microsoft Windows

Operating System: Windows 2000 or later.

This line needs to be near the top of the file (or in stdafx.h, if you use one.)

#include <afx.h>

You need a variable of type HANDLE with the same lifetime as your program. Perhaps as a member of your CWinApp object.

HANDLE mutex;

At the earliest possible point in your program, you need to initialize it and perform your check. "MyApp" should be a string unique to your application. See here for full details.

mutex = CreateMutex( NULL, TRUE, "MyApp" );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
     // There's another instance running.  What do you do?
}

Finally, near the end of your program, you need to close the mutex.

CloseHandle( mutex );

Python

Language Version: 2.5

without `inspect`

import sys, os

def isOnlyInstance():
    frame = sys._getframe()
    while frame.f_back:
        frame = frame.f_back
    if not os.system('(( $(ps -aef | grep python | grep \'[' + frame.f_code.co_filename[0] + \
    ']' + frame.f_code.co_filename[1:len(frame.f_code.co_filename)] + '\' | wc \
    -l) > 1 ))'):
        return 0
    else:
        return 1

with `inspect`

import inspect, os

def isOnlyInstance()
    frame = inspect.currentframe()
    while frame.f_back:
        frame = frame.f_back
    if not os.system('(( $(ps -aef | grep python | grep \'[' + frame.f_code.co_filename[0] + \
    ']' + frame.f_code.co_filename[1:len(frame.f_code.co_filename)] + '\' | wc \
    -l) > 1 ))'):
        return 0
    else:
        return 1

Visual Basic

Language Version: 6

 Dim onlyInstance as Boolean
 onlyInstance = not App.PrevInstance