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
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_SNGLINST_H_
13 #define _WX_SNGLINST_H_
15 #if wxUSE_SNGLINST_CHECKER
20 // ----------------------------------------------------------------------------
21 // wxSingleInstanceChecker
22 // ----------------------------------------------------------------------------
24 class WXDLLIMPEXP_BASE wxSingleInstanceChecker
27 // default ctor, use Create() after it
28 wxSingleInstanceChecker() { Init(); }
30 // like Create() but no error checking (dangerous!)
31 wxSingleInstanceChecker(const wxString
& name
,
32 const wxString
& path
= wxEmptyString
)
38 // notice that calling Create() is optional now, if you don't do it before
39 // calling IsAnotherRunning(), CreateDefault() is used automatically
41 // name it is used as the mutex name under Win32 and the lock file name
42 // under Unix so it should be as unique as possible and must be non-empty
44 // path is optional and is ignored under Win32 and used as the directory to
45 // create the lock file in under Unix (default is wxGetHomeDir())
47 // returns false if initialization failed, it doesn't mean that another
48 // instance is running - use IsAnotherRunning() to check it
49 bool Create(const wxString
& name
, const wxString
& path
= wxEmptyString
);
51 // use the default name, which is a combination of wxTheApp->GetAppName()
52 // and wxGetUserId() for mutex/lock file
54 // this is called implicitly by IsAnotherRunning() if the checker hadn't
55 // been created until then
58 wxCHECK_MSG( wxTheApp
, false, "must have application instance" );
59 return Create(wxTheApp
->GetAppName() + '-' + wxGetUserId());
62 // is another copy of this program already running?
63 bool IsAnotherRunning() const
67 if ( !const_cast<wxSingleInstanceChecker
*>(this)->CreateDefault() )
69 // if creation failed, return false as it's better to not
70 // prevent this instance from starting up if there is an error
75 return DoIsAnotherRunning();
78 // dtor is not virtual, this class is not meant to be used polymorphically
79 ~wxSingleInstanceChecker();
82 // common part of all ctors
83 void Init() { m_impl
= NULL
; }
85 // do check if another instance is running, called only if m_impl != NULL
86 bool DoIsAnotherRunning() const;
88 // the implementation details (platform specific)
89 class WXDLLIMPEXP_FWD_BASE wxSingleInstanceCheckerImpl
*m_impl
;
91 wxDECLARE_NO_COPY_CLASS(wxSingleInstanceChecker
);
94 #endif // wxUSE_SNGLINST_CHECKER
96 #endif // _WX_SNGLINST_H_