]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/snglinst.h
undo the last change as it results in buildbot configuration error
[wxWidgets.git] / interface / wx / snglinst.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: snglinst.h
e54c96f1 3// Purpose: interface of wxSingleInstanceChecker
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
9/**
10 @class wxSingleInstanceChecker
7c913512 11
23324ae1 12 wxSingleInstanceChecker class allows to check that only a single instance of a
e725ba4f
FM
13 program is running.
14
15 To do it, you should create an object of this class. As long as this object
16 is alive, calls to wxSingleInstanceChecker::IsAnotherRunning from other
17 processes will return @true.
7c913512 18
23324ae1 19 As the object should have the life span as big as possible, it makes sense to
e725ba4f
FM
20 create it either as a global or in wxApp::OnInit.
21 For example:
7c913512 22
23324ae1
FM
23 @code
24 bool MyApp::OnInit()
25 {
26 const wxString name = wxString::Format("MyApp-%s", wxGetUserId().c_str());
27 m_checker = new wxSingleInstanceChecker(name);
28 if ( m_checker-IsAnotherRunning() )
29 {
30 wxLogError(_("Another program instance is already running, aborting."));
7c913512 31
6bfc18d0
VZ
32 delete m_checker; // OnExit() won't be called if we return false
33 m_checker = NULL;
7c913512 34
6bfc18d0 35 return false;
23324ae1 36 }
7c913512 37
23324ae1 38 ... more initializations ...
7c913512 39
6bfc18d0 40 return true;
23324ae1 41 }
7c913512 42
23324ae1
FM
43 int MyApp::OnExit()
44 {
45 delete m_checker;
7c913512 46
23324ae1
FM
47 return 0;
48 }
49 @endcode
7c913512 50
e725ba4f
FM
51 Note using wxGetUserId() to construct the name: this allows different user
52 to run the application concurrently which is usually the intended goal.
53 If you don't use the user name in the wxSingleInstanceChecker name, only
54 one user would be able to run the application at a time.
7c913512 55
23324ae1
FM
56 This class is implemented for Win32 and Unix platforms (supporting @c fcntl()
57 system call, but almost all of modern Unix systems do) only.
7c913512 58
23324ae1 59 @library{wxbase}
3c99e2fd 60 @category{appmanagement}
23324ae1 61*/
7c913512 62class wxSingleInstanceChecker
23324ae1
FM
63{
64public:
65 /**
e725ba4f
FM
66 Default ctor, use Create() after it.
67 */
68 wxSingleInstanceChecker();
69
70 /**
71 Like Create() but without error checking.
23324ae1
FM
72 */
73 wxSingleInstanceChecker(const wxString& name,
74 const wxString& path = wxEmptyString);
75
76 /**
77 Destructor frees the associated resources.
e725ba4f 78 Note that it is not virtual, this class is not meant to be used polymorphically.
23324ae1
FM
79 */
80 ~wxSingleInstanceChecker();
81
82 /**
83 Initialize the object if it had been created using the default constructor.
7c913512 84 Note that you can't call Create() more than once, so calling it if the
e725ba4f 85 @ref wxSingleInstanceChecker() "non default ctor" had been used is an error.
3c4f71cc 86
7c913512 87 @param name
e725ba4f 88 Must be given and be as unique as possible. It is used as the
4cc4bfaf 89 mutex name under Win32 and the lock file name under Unix.
e725ba4f
FM
90 GetAppName() and wxGetUserId() are commonly used to construct
91 this parameter.
7c913512 92 @param path
e725ba4f
FM
93 The path is optional and is ignored under Win32 and used as the
94 directory to create the lock file in under Unix
95 (default is wxGetHomeDir()).
3c4f71cc 96
d29a9a8a 97 @return Returns @false if initialization failed, it doesn't mean that
e725ba4f
FM
98 another instance is running - use IsAnotherRunning() to check
99 for it.
100
101 @note
102 One of possible reasons while Create may fail on Unix is that the lock
103 file used for checking already exists but was not created by the user.
104 Therefore applications shouldn't treat failure of this function as fatal
105 condition, because doing so would open them to the possibility of a
106 Denial of Service attack. Instead, they should alert the user about
107 the problem and offer to continue execution without checking if
108 another instance is running.
23324ae1
FM
109 */
110 bool Create(const wxString& name,
111 const wxString& path = wxEmptyString);
112
113 /**
114 Returns @true if another copy of this program is already running, @false
115 otherwise.
116 */
328f5751 117 bool IsAnotherRunning() const;
23324ae1 118};
e54c96f1 119