2008-03-08 13:52:38 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: snglinst.h
|
2008-03-10 15:24:38 +00:00
|
|
|
// Purpose: interface of wxSingleInstanceChecker
|
2008-03-08 13:52:38 +00:00
|
|
|
// Author: wxWidgets team
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
@class wxSingleInstanceChecker
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
wxSingleInstanceChecker class allows to check that only a single instance of a
|
2008-10-05 11:24:00 +00:00
|
|
|
program is running.
|
|
|
|
|
|
|
|
To do it, you should create an object of this class. As long as this object
|
|
|
|
is alive, calls to wxSingleInstanceChecker::IsAnotherRunning from other
|
|
|
|
processes will return @true.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
As the object should have the life span as big as possible, it makes sense to
|
2008-10-05 11:24:00 +00:00
|
|
|
create it either as a global or in wxApp::OnInit.
|
|
|
|
For example:
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@code
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
|
|
|
const wxString name = wxString::Format("MyApp-%s", wxGetUserId().c_str());
|
|
|
|
m_checker = new wxSingleInstanceChecker(name);
|
|
|
|
if ( m_checker-IsAnotherRunning() )
|
|
|
|
{
|
|
|
|
wxLogError(_("Another program instance is already running, aborting."));
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-07-02 14:45:57 +00:00
|
|
|
delete m_checker; // OnExit() won't be called if we return false
|
|
|
|
m_checker = NULL;
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-07-02 14:45:57 +00:00
|
|
|
return false;
|
2008-03-08 13:52:38 +00:00
|
|
|
}
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
... more initializations ...
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-07-02 14:45:57 +00:00
|
|
|
return true;
|
2008-03-08 13:52:38 +00:00
|
|
|
}
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
int MyApp::OnExit()
|
|
|
|
{
|
|
|
|
delete m_checker;
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
@endcode
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-10-05 11:24:00 +00:00
|
|
|
Note using wxGetUserId() to construct the name: this allows different user
|
|
|
|
to run the application concurrently which is usually the intended goal.
|
|
|
|
If you don't use the user name in the wxSingleInstanceChecker name, only
|
|
|
|
one user would be able to run the application at a time.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
This class is implemented for Win32 and Unix platforms (supporting @c fcntl()
|
|
|
|
system call, but almost all of modern Unix systems do) only.
|
2008-03-08 14:43:31 +00:00
|
|
|
|
2008-03-08 13:52:38 +00:00
|
|
|
@library{wxbase}
|
|
|
|
@category{misc}
|
|
|
|
*/
|
2008-03-08 14:43:31 +00:00
|
|
|
class wxSingleInstanceChecker
|
2008-03-08 13:52:38 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2008-10-05 11:24:00 +00:00
|
|
|
Default ctor, use Create() after it.
|
|
|
|
*/
|
|
|
|
wxSingleInstanceChecker();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Like Create() but without error checking.
|
2008-03-08 13:52:38 +00:00
|
|
|
*/
|
|
|
|
wxSingleInstanceChecker(const wxString& name,
|
|
|
|
const wxString& path = wxEmptyString);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Destructor frees the associated resources.
|
2008-10-05 11:24:00 +00:00
|
|
|
Note that it is not virtual, this class is not meant to be used polymorphically.
|
2008-03-08 13:52:38 +00:00
|
|
|
*/
|
|
|
|
~wxSingleInstanceChecker();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Initialize the object if it had been created using the default constructor.
|
2008-03-08 14:43:31 +00:00
|
|
|
Note that you can't call Create() more than once, so calling it if the
|
2008-10-05 11:24:00 +00:00
|
|
|
@ref wxSingleInstanceChecker() "non default ctor" had been used is an error.
|
2008-03-20 13:45:17 +00:00
|
|
|
|
2008-03-08 14:43:31 +00:00
|
|
|
@param name
|
2008-10-05 11:24:00 +00:00
|
|
|
Must be given and be as unique as possible. It is used as the
|
2008-03-09 12:33:59 +00:00
|
|
|
mutex name under Win32 and the lock file name under Unix.
|
2008-10-05 11:24:00 +00:00
|
|
|
GetAppName() and wxGetUserId() are commonly used to construct
|
|
|
|
this parameter.
|
2008-03-08 14:43:31 +00:00
|
|
|
@param path
|
2008-10-05 11:24:00 +00:00
|
|
|
The path is optional and is ignored under Win32 and used as the
|
|
|
|
directory to create the lock file in under Unix
|
|
|
|
(default is wxGetHomeDir()).
|
2008-03-20 13:45:17 +00:00
|
|
|
|
2008-05-11 01:38:53 +00:00
|
|
|
@return Returns @false if initialization failed, it doesn't mean that
|
2008-10-05 11:24:00 +00:00
|
|
|
another instance is running - use IsAnotherRunning() to check
|
|
|
|
for it.
|
|
|
|
|
|
|
|
@note
|
|
|
|
One of possible reasons while Create may fail on Unix is that the lock
|
|
|
|
file used for checking already exists but was not created by the user.
|
|
|
|
Therefore applications shouldn't treat failure of this function as fatal
|
|
|
|
condition, because doing so would open them to the possibility of a
|
|
|
|
Denial of Service attack. Instead, they should alert the user about
|
|
|
|
the problem and offer to continue execution without checking if
|
|
|
|
another instance is running.
|
2008-03-08 13:52:38 +00:00
|
|
|
*/
|
|
|
|
bool Create(const wxString& name,
|
|
|
|
const wxString& path = wxEmptyString);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns @true if another copy of this program is already running, @false
|
|
|
|
otherwise.
|
|
|
|
*/
|
2008-03-09 16:24:26 +00:00
|
|
|
bool IsAnotherRunning() const;
|
2008-03-08 13:52:38 +00:00
|
|
|
};
|
2008-03-10 15:24:38 +00:00
|
|
|
|