]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: snglinst.h | |
3 | // Purpose: interface of wxSingleInstanceChecker | |
4 | // Author: wxWidgets team | |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxSingleInstanceChecker | |
11 | ||
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 | |
14 | long as this object is alive, calls to | |
15 | wxSingleInstanceChecker::IsAnotherRunning from | |
16 | other processes will return @true. | |
17 | ||
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: | |
21 | ||
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.")); | |
30 | ||
31 | delete m_checker; // OnExit() won't be called if we return false | |
32 | m_checker = NULL; | |
33 | ||
34 | return false; | |
35 | } | |
36 | ||
37 | ... more initializations ... | |
38 | ||
39 | return true; | |
40 | } | |
41 | ||
42 | int MyApp::OnExit() | |
43 | { | |
44 | delete m_checker; | |
45 | ||
46 | return 0; | |
47 | } | |
48 | @endcode | |
49 | ||
50 | Note using wxGetUserId() to construct the name: this | |
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. | |
54 | ||
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. | |
57 | ||
58 | @library{wxbase} | |
59 | @category{misc} | |
60 | */ | |
61 | class wxSingleInstanceChecker | |
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. | |
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. | |
79 | Note that you can't call Create() more than once, so calling it if the | |
80 | @ref wxsingleinstancechecker() "non default ctor" | |
81 | had been used is an error. | |
82 | ||
83 | @param name | |
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. | |
88 | @param path | |
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()) | |
92 | ||
93 | @return Returns @false if initialization failed, it doesn't mean that | |
94 | another instance is running - use IsAnotherRunning() | |
95 | to check for it. | |
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 | */ | |
104 | bool IsAnotherRunning() const; | |
105 | }; | |
106 |