One instance running (Phoenix)

Keywords : Single instance checker, One instance, Get user id, Application.


Introduction

Sometimes you want to ensure, that only one instance of your application is running.

There is class for achieving this. It is called wx.SingleInstanceChecker.


What Objects are Involved

* wx.SingleInstanceChecker


Process Overview

First, you define a name. It must be carefully chosen.

Normally you need to prevent running more instances of o program for one user, but you want to allow another user to run it concurrently.

 self.name = "SingleApp-%s" % wx.GetUserId() 

After that, you need to create an object out of this class.

 self.instance = wx.SingleInstanceChecker(self.name) 

If desired you can give a second parameter containing the path to use for the lock file.

By default, it will be created in user's home directory.

You check whether an instance of an application is running with the  IsAnotherRunnig()  method.

If yes, you should close the application because another is already running.

We create a destructor in order to delete that lock file, when the application ends.

If we don't do this, each time you start your application a message dialog informs you that an old lock file has been deleted.

Such behavior is really annoying and practically unusable in real world applications.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one

img_sample_one.png

   1 # sample_one.py
   2 
   3 import wx
   4 
   5 # class SingleAppFrame
   6 # class SingleApp
   7 
   8 #---------------------------------------------------------------------------
   9 
  10 class SingleAppFrame(wx.Frame):
  11     """
  12     ...
  13     """
  14     def __init__(self, parent, title):
  15         wx.Frame.__init__(self, parent, title=title, size=(400, 250))
  16 
  17         self.SetIcon(wx.Icon('icons/wxwin.ico'))
  18 
  19         #------------
  20 
  21         self.panel = wx.Panel(self, -1, (0, 0), (400 , 200))
  22         self.panel.SetBackgroundColour("#eceade")
  23 
  24         #------------
  25 
  26         self.sizer = wx.BoxSizer(wx.VERTICAL)
  27         self.sizer.Add(self.panel, 1, wx.EXPAND )
  28 
  29         self.SetSizer(self.sizer)
  30         self.SetAutoLayout(True)
  31         self.sizer.Fit(self)
  32         self.Centre()
  33 
  34 #---------------------------------------------------------------------------
  35 
  36 class SingleApp(wx.App):
  37     """
  38     ...
  39     """
  40     def OnInit(self):
  41 
  42         #------------
  43 
  44         self.name = "SingleApp-%s" % wx.GetUserId()
  45         self.instance = wx.SingleInstanceChecker(self.name)
  46 
  47         if self.instance.IsAnotherRunning():
  48             wx.MessageBox("Another instance is running.", "ERROR")
  49             return False
  50 
  51         #------------
  52 
  53         frame = SingleAppFrame(None, "Single application")
  54         frame.Show()
  55 
  56         return True
  57 
  58 #---------------------------------------------------------------------------
  59 
  60 def main():
  61     app = SingleApp(redirect=False)
  62     app.MainLoop()
  63 
  64 #---------------------------------------------------------------------------
  65 
  66 if __name__ == "__main__" :
  67     main()


Download source

source.zip


Additional Information

Link :

# Old source code :

https://www.daniweb.com/programming/software-development/threads/128350/starting-wxpython-gui-code

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Jan bodnar, Vegaseat (DaniWeb), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

14/06/20 - Ecco (Updated page for wxPython Phoenix).


Comments

- blah, blah, blah...

One instance running (Phoenix) (last edited 2020-10-07 10:17:59 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.