]> git.saurik.com Git - wxWidgets.git/blame - interface/snglinst.h
More initial reviews of [u*-v*] interface headers.
[wxWidgets.git] / interface / 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
11 @wxheader{snglinst.h}
7c913512 12
23324ae1
FM
13 wxSingleInstanceChecker class allows to check that only a single instance of a
14 program is running. To do it, you should create an object of this class. As
7c913512 15 long as this object is alive, calls to
23324ae1
FM
16 wxSingleInstanceChecker::IsAnotherRunning from
17 other processes will return @true.
7c913512 18
23324ae1
FM
19 As the object should have the life span as big as possible, it makes sense to
20 create it either as a global or in wxApp::OnInit. For
21 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
23324ae1
FM
32 delete m_checker; // OnExit() won't be called if we return @false
33 m_checker = @NULL;
7c913512 34
23324ae1
FM
35 return @false;
36 }
7c913512 37
23324ae1 38 ... more initializations ...
7c913512 39
23324ae1
FM
40 return @true;
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
e54c96f1 51 Note using wxGetUserId() to construct the name: this
23324ae1
FM
52 allows different user to run the application concurrently which is usually the
53 intended goal. If you don't use the user name in the wxSingleInstanceChecker
54 name, only 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
FM
59 @library{wxbase}
60 @category{misc}
61*/
7c913512 62class wxSingleInstanceChecker
23324ae1
FM
63{
64public:
65 /**
66 Like Create() but without
67 error checking.
68 */
69 wxSingleInstanceChecker(const wxString& name,
70 const wxString& path = wxEmptyString);
71
72 /**
73 Destructor frees the associated resources.
23324ae1
FM
74 Note that it is not virtual, this class is not meant to be used polymorphically
75 */
76 ~wxSingleInstanceChecker();
77
78 /**
79 Initialize the object if it had been created using the default constructor.
7c913512
FM
80 Note that you can't call Create() more than once, so calling it if the
81 @ref wxsingleinstancechecker() "non default ctor"
23324ae1 82 had been used is an error.
3c4f71cc 83
7c913512 84 @param name
4cc4bfaf
FM
85 must be given and be as unique as possible. It is used as the
86 mutex name under Win32 and the lock file name under Unix.
87 GetAppName() and wxGetUserId()
88 are commonly used to construct this parameter.
7c913512 89 @param path
4cc4bfaf
FM
90 is optional and is ignored under Win32 and used as the directory to
91 create the lock file in under Unix (default is
92 wxGetHomeDir())
3c4f71cc 93
23324ae1 94 @returns Returns @false if initialization failed, it doesn't mean that
4cc4bfaf
FM
95 another instance is running - use IsAnotherRunning()
96 to check for it.
23324ae1
FM
97 */
98 bool Create(const wxString& name,
99 const wxString& path = wxEmptyString);
100
101 /**
102 Returns @true if another copy of this program is already running, @false
103 otherwise.
104 */
328f5751 105 bool IsAnotherRunning() const;
23324ae1 106};
e54c96f1 107