I suggest Win32::Mutex as a way to guarantee only one instance of your Win32 program runs at a time. I have been using Win32::Semaphore but have learned that Inno Setup supports Mutexes (AppMutex). This allows my install to detect if my program is already running during install/uninstall and prompts the user to first shut down the app before continuing. For the benefit of others that may have the same need, I have included code for both.

Mutex Code

#!perl -w use strict; $|=1; use Win32::Mutex; my $mutexname="PIGMania"; if(Win32::Mutex->open($mutexname)){ print "must be already running"; exit; } my $mutex=Win32::Mutex->new(1,$mutexname); $mutex->wait(3); print "Mutex: $mutex\n"; my $x=30; while($x){ print "."; select(undef,undef,undef,1.5); $x--; }

Semaphore Code

#!perl -w use strict; $|=1; use Win32::Semaphore; my $semaphore_name="PIGMania"; if(Win32::Semaphore->open($semaphore_name)){ print "$semaphore_name is already running\n"; exit; } my $semaphore=Win32::Semaphore->new(1,1,$semaphore_name); print "Semaphore: $semaphore\n"; my $x=30; while($x){ print "."; select(undef,undef,undef,1.5); $x--; }

-------------------------------
Need a good Perl friendly Host Provider?
http://www.dreamhost.com


In reply to Using Win32::Mutex to guarantee only one process will run at a time. by slloyd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.