Just another note for future archive searchers....
On Windows, wx.SingleInstanceChecker seems to work just as advertised -- adding the following to my wx.App subclass did what I wanted:
def OnInit(self):
# ...GetAppName() and self.userdata_dir defined above...
# Check for another instance running, and exit if found.
app_user_name = self.GetAppName() + "-" + wx.GetUserId()
self.single_instance = wx.SingleInstanceChecker(app_user_name,
self.userdata_dir)
if self.single_instance.IsAnotherRunning():
self.logger.warning("App is exiting due to multiple " +
"instances running at once.")
return False
# ...
On Mac, I found that the code above caused a warning dialog to appear each time the app was run after the first, even if no other instance was running. The warning alerted the user that a stale lock file had been found and removed, and it appeared every time the app started. (This is on Darwin 8.6.1; Python version 2.4.1; wxPython version 2.6.3.2.) I found that adding this OnExit method to my wx.App subclass fixed the problem:
def OnExit(self):
# Force cleanup of the single instance checker.
del self.single_instance
With that, I no longer get warning dialogs.
Also, I noticed that the Mac implementation leaves a lockfile in /Users/<username> by default, which seems "messy" to me, so I added the path shown in OnInit, above, to put the lockfile in the user data directory (as returned by wx.StandardPaths.Get().GetUserDataDir()). That seems cleaner.
This works on both platforms I'm testing (Win2k and MacOS 10.4/Intel). I'll post a wiki page on this unless I hear from someone that I've broken anything above; alternatively I can add this as a bug report if that would be better.
Just another note for future archive searchers....
On Windows, wx.SingleInstanceChecker seems to work just as advertised -- adding the following to my wx.App subclass did what I wanted:
def OnInit(self):
# ...GetAppName() and self.userdata_dir defined above...
# Check for another instance running, and exit if found.
app_user_name = self.GetAppName() + "-" + wx.GetUserId()
self.single_instance = wx.SingleInstanceChecker(app_user_name,
self.userdata_dir)
if self.single_instance.IsAnotherRunning():
self.logger.warning("App is exiting due to multiple " +
"instances running at once.")
return False
# ...
On Mac, I found that the code above caused a warning dialog to appear each time the app was run after the first, even if no other instance was running. The warning alerted the user that a stale lock file had been found and removed, and it appeared every time the app started. (This is on Darwin 8.6.1; Python version 2.4.1; wxPython version 2.6.3.2.) I found that adding this OnExit method to my wx.App subclass fixed the problem:
def OnExit(self):
# Force cleanup of the single instance checker.
del self.single_instance
With that, I no longer get warning dialogs.
I wouldn't think that this would be necessary, but I guess it is possible that the automatic cleanup of the app object instance is happening too late and that the SingleInstanceChecker on Mac requires that it be destroyed before the cleanup of the wx lib happens. So this is a good workaround.
Also, I noticed that the Mac implementation leaves a lockfile in /Users/<username> by default, which seems "messy" to me, so I added the path shown in OnInit, above, to put the lockfile in the user data directory (as returned by wx.StandardPaths.Get().GetUserDataDir()). That seems cleaner.
This works on both platforms I'm testing (Win2k and MacOS 10.4/Intel). I'll post a wiki page on this unless I hear from someone that I've broken anything above; alternatively I can add this as a bug report if that would be better.
Please do make a wiki page for it, this is good information and should be published.
Thanks!
···
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!