]> git.saurik.com Git - wxWidgets.git/blob - interface/snglinst.h
Probable fix for [ 1933745 ] Crash in wxDataViewHeaderWindowBase::GetColumn()
[wxWidgets.git] / interface / 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 @wxheader{snglinst.h}
12
13 wxSingleInstanceChecker class allows to check that only a single instance of a
14 program is running. To do it, you should create an object of this class. As
15 long as this object is alive, calls to
16 wxSingleInstanceChecker::IsAnotherRunning from
17 other 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. For
21 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
52 allows different user to run the application concurrently which is usually the
53 intended goal. If you don't use the user name in the wxSingleInstanceChecker
54 name, only 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 Like Create() but without
67 error checking.
68 */
69 wxSingleInstanceChecker(const wxString& name,
70 const wxString& path = wxEmptyString);
71
72 /**
73 Destructor frees the associated resources.
74 Note that it is not virtual, this class is not meant to be used polymorphically
75 */
76 ~wxSingleInstanceChecker();
77
78 /**
79 Initialize the object if it had been created using the default constructor.
80 Note that you can't call Create() more than once, so calling it if the
81 @ref wxsingleinstancechecker() "non default ctor"
82 had been used is an error.
83
84 @param name
85 must be given and be as unique as possible. It is used as the
86 mutex name under Win32 and the lock file name under Unix.
87 GetAppName() and wxGetUserId()
88 are commonly used to construct this parameter.
89 @param path
90 is optional and is ignored under Win32 and used as the directory to
91 create the lock file in under Unix (default is
92 wxGetHomeDir())
93
94 @returns Returns @false if initialization failed, it doesn't mean that
95 another instance is running - use IsAnotherRunning()
96 to check for it.
97 */
98 bool Create(const wxString& name,
99 const wxString& path = wxEmptyString);
100
101 /**
102 Returns @true if another copy of this program is already running, @false
103 otherwise.
104 */
105 bool IsAnotherRunning() const;
106 };
107