]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/snglinst.h
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     interface of wxSingleInstanceChecker 
   4 // Author:      wxWidgets team 
   6 // Licence:     wxWindows license 
   7 ///////////////////////////////////////////////////////////////////////////// 
  10     @class wxSingleInstanceChecker 
  12     wxSingleInstanceChecker class allows to check that only a single instance of a 
  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. 
  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. 
  26         const wxString name = wxString::Format("MyApp-%s", wxGetUserId().c_str()); 
  27         m_checker = new wxSingleInstanceChecker(name); 
  28         if ( m_checker-IsAnotherRunning() ) 
  30             wxLogError(_("Another program instance is already running, aborting.")); 
  32             delete m_checker; // OnExit() won't be called if we return false 
  38         ... more initializations ... 
  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. 
  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. 
  62 class wxSingleInstanceChecker
 
  66         Default ctor, use Create() after it. 
  68     wxSingleInstanceChecker(); 
  71         Like Create() but without error checking. 
  73     wxSingleInstanceChecker(const wxString
& name
, 
  74                             const wxString
& path 
= wxEmptyString
); 
  77         Destructor frees the associated resources. 
  78         Note that it is not virtual, this class is not meant to be used polymorphically. 
  80     ~wxSingleInstanceChecker(); 
  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. 
  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 
  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()). 
  97         @return Returns @false if initialization failed, it doesn't mean that 
  98                 another instance is running - use  IsAnotherRunning() to check 
 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. 
 110     bool Create(const wxString
& name
, 
 111                 const wxString
& path 
= wxEmptyString
); 
 114         Returns @true if another copy of this program is already running, @false 
 117     bool IsAnotherRunning() const;