Re: Single Application Instance Example



Chris Lambacher wrote:
Does anyone know of an example of how to make a Python program only
run a single instance.  I am specifically looking for examples for
win32.

The Python Cookbook, Second Edition, page 380, recipe 9.9: Determining Whether Another Instance of a Script is Already Running in Windows, suggests using a mutex kernel object to be safe from race conditions:


from win32event import CreateMutex
from win32api import GetLastError
from winerror import ERROR_ALREADY_EXISTS
from sys import exit
handle = CreateMutex(None, 1, 'A unique mutex name')
if GetLastError() == ERROR_ALREADY_EXISTS:
	print "I exist already"
	exit(1)
else:
	print "I don't exist already"
	

I think I should be able to do this with win32all but the method is
not obvious.  Preferably I would like to be able to get a handle to
the already running instance so that I can push it to the foreground
as well.

There are ways using win32 to get the handle to the existing process, but I'm too rusty with win32 to offer that part of the solution.



-- Paul McNett http://paulmcnett.com

.



Relevant Pages

  • Re: Win32 TSR program skeleton
    ... under windows a "comsole" is just a thing used for display. ... are not avaible to Win32 apps, and are not created for Win32 console ... I need to write a TSR that does some periodical checks over another ... You should say that I can simple pipe the output to my app, but, ...
    (microsoft.public.win32.programmer.kernel)
  • Re: I really do like OS X but . . .
    ... Win32 is just an environment, ... >Windows right now, would be from .NET or Java. ... >Microsoft took an excellent kernel like NT, ... Only Apple. ...
    (comp.sys.mac.advocacy)
  • Re: linux to windows porting help
    ... Presently i am working on win32 console applications. ... PostThreadMessage - for Message queue between process and its ... donn have any windows handle so unable to use these functions... ...
    (comp.programming)
  • Re: As programmers...have we come a long way since 1993?
    ... If 'win64', as the main OS of PC users, ... is notably different in compatibility with old win32 software then ... the case Win64 should be largely tolerant of win32, ... > Look at Windows XP and Windows 2000, with that very scary DCOM exploit. ...
    (comp.programming)
  • Re: OT
    ... Für Windows Forms unter Windows ist das Verhalten aber naheliegend, da Windows Forms eben von Microsoft nur für Windows unterstützt werden und da eine Kapselung der Win32-Benutzerschnittstellen darstellen. ... Windos Forms is just a nice Frontend to Win32 but everything that worked in Win32 will work in WindowsForms too ... es darf nur die Framework-Doku ... das Framework sagt nichts in der Dokumentation ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)