我使用 py2exe 创建了一个 exe。但是,我能够打开我的 exe 的多个实例。如何确保一次只运行一个 exe 实例。我注意到 Dropbox 使用 py2exe 实现了这一点。
请您参考如下方法:
这是最终奏效的解决方案。 pywin32 中可用的互斥量完全符合要求。
from win32event import CreateMutex
from win32api import CloseHandle, GetLastError
from winerror import ERROR_ALREADY_EXISTS
class 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)
