]>
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$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxSingleInstanceChecker | |
7c913512 | 11 | |
23324ae1 FM |
12 | wxSingleInstanceChecker class allows to check that only a single instance of a |
13 | program is running. To do it, you should create an object of this class. As | |
7c913512 | 14 | long as this object is alive, calls to |
23324ae1 FM |
15 | wxSingleInstanceChecker::IsAnotherRunning from |
16 | other processes will return @true. | |
7c913512 | 17 | |
23324ae1 FM |
18 | As the object should have the life span as big as possible, it makes sense to |
19 | create it either as a global or in wxApp::OnInit. For | |
20 | example: | |
7c913512 | 21 | |
23324ae1 FM |
22 | @code |
23 | bool MyApp::OnInit() | |
24 | { | |
25 | const wxString name = wxString::Format("MyApp-%s", wxGetUserId().c_str()); | |
26 | m_checker = new wxSingleInstanceChecker(name); | |
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 | |
e54c96f1 | 50 | Note using wxGetUserId() to construct the name: this |
23324ae1 FM |
51 | allows different user to run the application concurrently which is usually the |
52 | intended goal. If you don't use the user name in the wxSingleInstanceChecker | |
53 | name, only one user would be able to run the application at a time. | |
7c913512 | 54 | |
23324ae1 FM |
55 | This class is implemented for Win32 and Unix platforms (supporting @c fcntl() |
56 | system call, but almost all of modern Unix systems do) only. | |
7c913512 | 57 | |
23324ae1 FM |
58 | @library{wxbase} |
59 | @category{misc} | |
60 | */ | |
7c913512 | 61 | class wxSingleInstanceChecker |
23324ae1 FM |
62 | { |
63 | public: | |
64 | /** | |
65 | Like Create() but without | |
66 | error checking. | |
67 | */ | |
68 | wxSingleInstanceChecker(const wxString& name, | |
69 | const wxString& path = wxEmptyString); | |
70 | ||
71 | /** | |
72 | Destructor frees the associated resources. | |
23324ae1 FM |
73 | Note that it is not virtual, this class is not meant to be used polymorphically |
74 | */ | |
75 | ~wxSingleInstanceChecker(); | |
76 | ||
77 | /** | |
78 | Initialize the object if it had been created using the default constructor. | |
7c913512 FM |
79 | Note that you can't call Create() more than once, so calling it if the |
80 | @ref wxsingleinstancechecker() "non default ctor" | |
23324ae1 | 81 | had been used is an error. |
3c4f71cc | 82 | |
7c913512 | 83 | @param name |
4cc4bfaf FM |
84 | must be given and be as unique as possible. It is used as the |
85 | mutex name under Win32 and the lock file name under Unix. | |
86 | GetAppName() and wxGetUserId() | |
87 | are commonly used to construct this parameter. | |
7c913512 | 88 | @param path |
4cc4bfaf FM |
89 | is optional and is ignored under Win32 and used as the directory to |
90 | create the lock file in under Unix (default is | |
91 | wxGetHomeDir()) | |
3c4f71cc | 92 | |
d29a9a8a | 93 | @return Returns @false if initialization failed, it doesn't mean that |
4cc4bfaf FM |
94 | another instance is running - use IsAnotherRunning() |
95 | to check for it. | |
23324ae1 FM |
96 | */ |
97 | bool Create(const wxString& name, | |
98 | const wxString& path = wxEmptyString); | |
99 | ||
100 | /** | |
101 | Returns @true if another copy of this program is already running, @false | |
102 | otherwise. | |
103 | */ | |
328f5751 | 104 | bool IsAnotherRunning() const; |
23324ae1 | 105 | }; |
e54c96f1 | 106 |