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