1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSingleInstanceChecker can be used to restrict the number of
4 // simultaneously running copies of a program to one
5 // Author: Vadim Zeitlin
9 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_SNGLINST_H_
14 #define _WX_SNGLINST_H_
16 #if wxUSE_SNGLINST_CHECKER
21 // ----------------------------------------------------------------------------
22 // wxSingleInstanceChecker
23 // ----------------------------------------------------------------------------
25 class WXDLLIMPEXP_BASE wxSingleInstanceChecker
28 // default ctor, use Create() after it
29 wxSingleInstanceChecker() { Init(); }
31 // like Create() but no error checking (dangerous!)
32 wxSingleInstanceChecker(const wxString
& name
,
33 const wxString
& path
= wxEmptyString
)
39 // notice that calling Create() is optional now, if you don't do it before
40 // calling IsAnotherRunning(), CreateDefault() is used automatically
42 // name it is used as the mutex name under Win32 and the lock file name
43 // under Unix so it should be as unique as possible and must be non-empty
45 // path is optional and is ignored under Win32 and used as the directory to
46 // create the lock file in under Unix (default is wxGetHomeDir())
48 // returns false if initialization failed, it doesn't mean that another
49 // instance is running - use IsAnotherRunning() to check it
50 bool Create(const wxString
& name
, const wxString
& path
= wxEmptyString
);
52 // use the default name, which is a combination of wxTheApp->GetAppName()
53 // and wxGetUserId() for mutex/lock file
55 // this is called implicitly by IsAnotherRunning() if the checker hadn't
56 // been created until then
59 wxCHECK_MSG( wxTheApp
, false, "must have application instance" );
60 return Create(wxTheApp
->GetAppName() + '-' + wxGetUserId());
63 // is another copy of this program already running?
64 bool IsAnotherRunning() const
68 if ( !const_cast<wxSingleInstanceChecker
*>(this)->CreateDefault() )
70 // if creation failed, return false as it's better to not
71 // prevent this instance from starting up if there is an error
76 return DoIsAnotherRunning();
79 // dtor is not virtual, this class is not meant to be used polymorphically
80 ~wxSingleInstanceChecker();
83 // common part of all ctors
84 void Init() { m_impl
= NULL
; }
86 // do check if another instance is running, called only if m_impl != NULL
87 bool DoIsAnotherRunning() const;
89 // the implementation details (platform specific)
90 class WXDLLIMPEXP_FWD_BASE wxSingleInstanceCheckerImpl
*m_impl
;
92 wxDECLARE_NO_COPY_CLASS(wxSingleInstanceChecker
);
95 #endif // wxUSE_SNGLINST_CHECKER
97 #endif // _WX_SNGLINST_H_