]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: snglinst.h | |
e54c96f1 | 3 | // Purpose: interface of wxSingleInstanceChecker |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
526954c5 | 6 | // Licence: wxWindows licence |
23324ae1 FM |
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 | { | |
956b3d92 | 26 | m_checker = new wxSingleInstanceChecker; |
23324ae1 FM |
27 | if ( m_checker-IsAnotherRunning() ) |
28 | { | |
29 | wxLogError(_("Another program instance is already running, aborting.")); | |
7c913512 | 30 | |
6bfc18d0 VZ |
31 | delete m_checker; // OnExit() won't be called if we return false |
32 | m_checker = NULL; | |
7c913512 | 33 | |
6bfc18d0 | 34 | return false; |
23324ae1 | 35 | } |
7c913512 | 36 | |
23324ae1 | 37 | ... more initializations ... |
7c913512 | 38 | |
6bfc18d0 | 39 | return true; |
23324ae1 | 40 | } |
7c913512 | 41 | |
23324ae1 FM |
42 | int MyApp::OnExit() |
43 | { | |
44 | delete m_checker; | |
7c913512 | 45 | |
23324ae1 FM |
46 | return 0; |
47 | } | |
48 | @endcode | |
7c913512 | 49 | |
956b3d92 VZ |
50 | Note that by default wxSingleInstanceChecker::CreateDefault() is used to |
51 | create the checker meaning that it will be initialized differently for | |
52 | different users and thus will allow different users to run the application | |
53 | concurrently which is usually the required behaviour. However if only a | |
54 | single instance of a program should be running system-wide, you need to | |
55 | call Create() with a custom name which does @em not include wxGetUserId(). | |
7c913512 | 56 | |
23324ae1 FM |
57 | This class is implemented for Win32 and Unix platforms (supporting @c fcntl() |
58 | system call, but almost all of modern Unix systems do) only. | |
7c913512 | 59 | |
23324ae1 | 60 | @library{wxbase} |
3c99e2fd | 61 | @category{appmanagement} |
23324ae1 | 62 | */ |
7c913512 | 63 | class wxSingleInstanceChecker |
23324ae1 FM |
64 | { |
65 | public: | |
66 | /** | |
956b3d92 VZ |
67 | Default constructor. |
68 | ||
69 | You may call Create() after using it or directly call | |
70 | IsAnotherRunning() in which case CreateDefault() will be implicitly | |
71 | used. | |
e725ba4f FM |
72 | */ |
73 | wxSingleInstanceChecker(); | |
74 | ||
75 | /** | |
956b3d92 VZ |
76 | Constructor calling Create(). |
77 | ||
78 | This constructor does exactly the same thing as Create() but doesn't | |
79 | allow to check for errors. | |
23324ae1 FM |
80 | */ |
81 | wxSingleInstanceChecker(const wxString& name, | |
82 | const wxString& path = wxEmptyString); | |
83 | ||
84 | /** | |
85 | Destructor frees the associated resources. | |
956b3d92 | 86 | |
e725ba4f | 87 | Note that it is not virtual, this class is not meant to be used polymorphically. |
23324ae1 FM |
88 | */ |
89 | ~wxSingleInstanceChecker(); | |
90 | ||
91 | /** | |
92 | Initialize the object if it had been created using the default constructor. | |
956b3d92 | 93 | |
7c913512 | 94 | Note that you can't call Create() more than once, so calling it if the |
956b3d92 | 95 | non default ctor had been used is an error. |
3c4f71cc | 96 | |
7c913512 | 97 | @param name |
e725ba4f | 98 | Must be given and be as unique as possible. It is used as the |
4cc4bfaf | 99 | mutex name under Win32 and the lock file name under Unix. |
956b3d92 | 100 | wxApp::GetAppName() and wxGetUserId() are commonly used to construct |
e725ba4f | 101 | this parameter. |
7c913512 | 102 | @param path |
e725ba4f FM |
103 | The path is optional and is ignored under Win32 and used as the |
104 | directory to create the lock file in under Unix | |
105 | (default is wxGetHomeDir()). | |
3c4f71cc | 106 | |
956b3d92 VZ |
107 | @return |
108 | Returns @false if initialization failed, it doesn't mean that | |
109 | another instance is running -- use IsAnotherRunning() to check for | |
110 | it. | |
e725ba4f FM |
111 | |
112 | @note | |
956b3d92 | 113 | One of possible reasons while Create() may fail on Unix is that the lock |
e725ba4f FM |
114 | file used for checking already exists but was not created by the user. |
115 | Therefore applications shouldn't treat failure of this function as fatal | |
116 | condition, because doing so would open them to the possibility of a | |
117 | Denial of Service attack. Instead, they should alert the user about | |
118 | the problem and offer to continue execution without checking if | |
119 | another instance is running. | |
23324ae1 FM |
120 | */ |
121 | bool Create(const wxString& name, | |
122 | const wxString& path = wxEmptyString); | |
123 | ||
124 | /** | |
956b3d92 VZ |
125 | Calls Create() with default name. |
126 | ||
127 | This method simply calls Create() with a string composed of | |
128 | wxApp::GetAppName() and wxGetUserId(). | |
129 | ||
130 | Because this method uses wxApp::GetAppName(), it may only be called | |
131 | after the global application was constructed. | |
132 | ||
133 | @since 2.9.1 | |
134 | */ | |
135 | bool CreateDefault(); | |
136 | ||
137 | /** | |
138 | Returns @true if another copy of this program is already running and | |
139 | @false otherwise. | |
140 | ||
141 | Notice that if the object was created using the default constructor | |
142 | Create() hadn't been called before this method, it will call | |
143 | CreateDefault() automatically. | |
23324ae1 | 144 | */ |
328f5751 | 145 | bool IsAnotherRunning() const; |
23324ae1 | 146 | }; |
e54c96f1 | 147 |