from win32event import CreateMutexfrom win32api import CloseHandle, GetLastErrorfrom winerror import ERROR_ALREADY_EXISTSclass singleinstance:""" Limits application to single instance """def __init__(self):self.mutexname = "testmutex_{D0E858DF-985E-4907-B7FB-8D732C3FC3B9}"self.mutex = CreateMutex(None, False, self.mutexname)self.lasterror = GetLastError()def aleradyrunning(self):return (self.lasterror == ERROR_ALREADY_EXISTS)def __del__(self):if self.mutex:CloseHandle(self.mutex)#---------------------------------------------## sample usage:#from singleinstance import singleinstancefrom sys import exit# do this at beginnig of your applicationmyapp = singleinstance()# check is another instance of same program runningif myapp.aleradyrunning():print "Another instance of this program is already running"exit(0)# not running, safe to continue...print "No another instance is running, can continue here"
Showing posts with label single instance. Show all posts
Showing posts with label single instance. Show all posts
Friday, August 20, 2010
Create Single Instance Python Application in Windows
Source by Dragan Jovelic.
Subscribe to:
Posts (Atom)