]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/snglinst.tex
added null pointer check and assert
[wxWidgets.git] / docs / latex / wx / snglinst.tex
CommitLineData
68a602fc
VZ
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2%% Name: snglinst.tex
3%% Purpose: wxSingleInstanceChecker documentation
4%% Author: Vadim Zeitlin
5%% Modified by:
6%% Created: 08.06.01
7%% RCS-ID: $Id$
8%% Copyright: (c) 2001 Vadim Zeitlin
8795498c 9%% License: wxWindows license
68a602fc
VZ
10%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11
12\section{\class{wxSingleInstanceChecker}}\label{wxsingleinstancechecker}
13
14wxSingleInstanceChecker class allows to check that only a single instance of a
15program is running. To do it, you should create an object of this class. As
16long as this object is alive, calls to
17\helpref{IsAnotherRunning()}{wxsingleinstancecheckerisanotherrunning} from
cc81d32f 18other processes will return {\tt true}.
68a602fc
VZ
19
20As the object should have the life span as big as possible, it makes sense to
21create it either as a global or in \helpref{wxApp::OnInit}{wxapponinit}. For
22example:
23
24\begin{verbatim}
25bool MyApp::OnInit()
26{
8d0a0c3f
VZ
27 const wxString name = wxString::Format("MyApp-%s", wxGetUserId().c_str());
28 m_checker = new wxSingleInstanceChecker(name);
68a602fc
VZ
29 if ( m_checker->IsAnotherRunning() )
30 {
31 wxLogError(_("Another program instance is already running, aborting."));
32
2e0d1932
VZ
33 delete m_checker; // OnExit() won't be called if we return false
34 m_checker = NULL;
35
cc81d32f 36 return false;
68a602fc
VZ
37 }
38
39 ... more initializations ...
40
cc81d32f 41 return true;
68a602fc
VZ
42}
43
44int MyApp::OnExit()
45{
46 delete m_checker;
47
48 return 0;
49}
50\end{verbatim}
51
8d0a0c3f
VZ
52Note using \helpref{wxGetUserId()}{wxgetuserid} to construct the name: this
53allows different user to run the application concurrently which is usually the
54intended goal. If you don't use the user name in the wxSingleInstanceChecker
55name, only one user would be able to run the application at a time.
56
57This class is implemented for Win32 and Unix platforms (supporting {\tt fcntl()}
58system call, but almost all of modern Unix systems do) only.
68a602fc
VZ
59
60\wxheading{Derived from}
61
62No base class
63
b78d046d
VZ
64\wxheading{Include files}
65
66<wx/snglinst.h>
67
a7af285d
VZ
68\wxheading{Library}
69
70\helpref{wxBase}{librarieslist}
71
68a602fc
VZ
72\latexignore{\rtfignore{\wxheading{Members}}}
73
74\membersection{wxSingleInstanceChecker::wxSingleInstanceChecker}\label{wxsingleinstancecheckerctor}
75
76\func{}{wxSingleInstanceChecker}{\void}
77
78Default ctor, use \helpref{Create()}{wxsingleinstancecheckercreate} after it.
79
80\membersection{wxSingleInstanceChecker::wxSingleInstanceChecker}\label{wxsingleinstancecheckerwxsingleinstancechecker}
81
82\func{}{wxSingleInstanceChecker}{\param{const wxString\& }{name}, \param{const wxString\& }{path = wxEmptyString}}
83
84Like \helpref{Create()}{wxsingleinstancecheckercreate} but without
85error checking.
86
87\membersection{wxSingleInstanceChecker::Create}\label{wxsingleinstancecheckercreate}
88
89\func{bool}{Create}{\param{const wxString\& }{name}, \param{const wxString\& }{path = wxEmptyString}}
90
91Initialize the object if it had been created using the default constructor.
92Note that you can't call Create() more than once, so calling it if the
93\helpref{non default ctor}{wxsingleinstancecheckerwxsingleinstancechecker}
94had been used is an error.
95
96\wxheading{Parameters}
97
bc00e715 98\docparam{name}{must be given and be as unique as possible. It is used as the
68a602fc 99mutex name under Win32 and the lock file name under Unix.
8d0a0c3f
VZ
100\helpref{GetAppName()}{wxappgetappname} and \helpref{wxGetUserId()}{wxgetuserid}
101are commonly used to construct this parameter.}
68a602fc
VZ
102
103\docparam{path}{is optional and is ignored under Win32 and used as the directory to
104create the lock file in under Unix (default is
7c415790 105\helpref{wxGetHomeDir()}{wxgethomedir})}
68a602fc
VZ
106
107\wxheading{Return value}
108
cc81d32f 109Returns {\tt false} if initialization failed, it doesn't mean that another
68a602fc
VZ
110instance is running - use
111\helpref{IsAnotherRunning()}{wxsingleinstancecheckerisanotherrunning} to check
112for it.
113
3c5487b1
VS
114\wxheading{Note}
115
116One of possible reasons while Create may fail on Unix is that the lock file
117used for checking already exists but was not created by the user.
118Therefore applications shouldn't treat failure of this function as fatal
119condition, because doing so would open them to the possibility of a Denial of
120Service attack. Instead, they should alert the user about the problem and
121offer to continue execution without checking if another instance is running.
122
68a602fc
VZ
123\membersection{wxSingleInstanceChecker::IsAnotherRunning}\label{wxsingleinstancecheckerisanotherrunning}
124
125\constfunc{bool}{IsAnotherRunning}{\void}
126
cc81d32f
VS
127Returns {\tt true} if another copy of this program is already running, {\tt
128false} otherwise.
68a602fc
VZ
129
130\membersection{wxSingleInstanceChecker::\destruct{wxSingleInstanceChecker}}\label{wxsingleinstancecheckerdtor}
131
132\func{}{\destruct{wxSingleInstanceChecker}}{\void}
133
134Destructor frees the associated resources.
135
136Note that it is not virtual, this class is not meant to be used polymorphically
137