other s*h interface revisions
[wxWidgets.git] / interface / wx / snglinst.h
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.
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.
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.
21 For 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 allows different user
52 to run the application concurrently which is usually the intended goal.
53 If you don't use the user name in the wxSingleInstanceChecker name, only
54 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 Default ctor, use Create() after it.
67 */
68 wxSingleInstanceChecker();
69
70 /**
71 Like Create() but without error checking.
72 */
73 wxSingleInstanceChecker(const wxString& name,
74 const wxString& path = wxEmptyString);
75
76 /**
77 Destructor frees the associated resources.
78 Note that it is not virtual, this class is not meant to be used polymorphically.
79 */
80 ~wxSingleInstanceChecker();
81
82 /**
83 Initialize the object if it had been created using the default constructor.
84 Note that you can't call Create() more than once, so calling it if the
85 @ref wxSingleInstanceChecker() "non default ctor" had been used is an error.
86
87 @param name
88 Must be given and be as unique as possible. It is used as the
89 mutex name under Win32 and the lock file name under Unix.
90 GetAppName() and wxGetUserId() are commonly used to construct
91 this parameter.
92 @param path
93 The path is optional and is ignored under Win32 and used as the
94 directory to create the lock file in under Unix
95 (default is wxGetHomeDir()).
96
97 @return Returns @false if initialization failed, it doesn't mean that
98 another instance is running - use IsAnotherRunning() to check
99 for it.
100
101 @note
102 One of possible reasons while Create may fail on Unix is that the lock
103 file used for checking already exists but was not created by the user.
104 Therefore applications shouldn't treat failure of this function as fatal
105 condition, because doing so would open them to the possibility of a
106 Denial of Service attack. Instead, they should alert the user about
107 the problem and offer to continue execution without checking if
108 another instance is running.
109 */
110 bool Create(const wxString& name,
111 const wxString& path = wxEmptyString);
112
113 /**
114 Returns @true if another copy of this program is already running, @false
115 otherwise.
116 */
117 bool IsAnotherRunning() const;
118 };
119