]> git.saurik.com Git - wxWidgets.git/blob - interface/snglinst.h
adjusted indentation with astyle; added Id keyword
[wxWidgets.git] / interface / snglinst.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: snglinst.h
3 // Purpose: documentation for wxSingleInstanceChecker class
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 @class wxSingleInstanceChecker
11 @wxheader{snglinst.h}
12
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
15 long as this object is alive, calls to
16 wxSingleInstanceChecker::IsAnotherRunning from
17 other processes will return @true.
18
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:
22
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."));
31
32 delete m_checker; // OnExit() won't be called if we return @false
33 m_checker = @NULL;
34
35 return @false;
36 }
37
38 ... more initializations ...
39
40 return @true;
41 }
42
43 int MyApp::OnExit()
44 {
45 delete m_checker;
46
47 return 0;
48 }
49 @endcode
50
51 Note using wxGetUserId to construct the name: this
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.
55
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.
58
59 @library{wxbase}
60 @category{misc}
61 */
62 class wxSingleInstanceChecker
63 {
64 public:
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.
74
75 Note that it is not virtual, this class is not meant to be used polymorphically
76 */
77 ~wxSingleInstanceChecker();
78
79 /**
80 Initialize the object if it had been created using the default constructor.
81 Note that you can't call Create() more than once, so calling it if the
82 @ref wxsingleinstancechecker() "non default ctor"
83 had been used is an error.
84
85 @param name
86 must be given and be as unique as possible. It is used as the
87 mutex name under Win32 and the lock file name under Unix.
88 GetAppName() and wxGetUserId()
89 are commonly used to construct this parameter.
90
91 @param path
92 is optional and is ignored under Win32 and used as the directory to
93 create the lock file in under Unix (default is
94 wxGetHomeDir())
95
96 @returns Returns @false if initialization failed, it doesn't mean that
97 another instance is running - use IsAnotherRunning()
98 to check for it.
99 */
100 bool Create(const wxString& name,
101 const wxString& path = wxEmptyString);
102
103 /**
104 Returns @true if another copy of this program is already running, @false
105 otherwise.
106 */
107 bool IsAnotherRunning();
108 };