Re: Only One Instance.

From: Rob Kennedy (me_at_privacy.net)
Date: 01/14/04


Date: Wed, 14 Jan 2004 15:09:45 -0600

Jamie wrote:
> i always found that if you can only have one instance of the app then
> you should Special Name the Main Form Class to something that should
> never exist, use the FINDWIndow in the Main Source before The
> TAPplication.initiate gets executed..

Or, you could simply create a mutex. Advantages:

1. The name of the mutex has no effect on anything else in your program,
so you can continue to name your objects however you want.

2. The mutex name is more likely to be unique within the system. You can
even use a GUID.

3. Mutex creation is atomic. It is impossible for two processes to
create like-named mutexes without knowing it. Creating a window, on the
other hand, is not atomic. If process A and process B are started
simultaneously, they might both call FindWindow before either one has
created the window that the other process is looking for.

> if you find the CLASS then you should use the SENDMESSAGE to that
> handle that you have receieved with a special user message that you have
> your app responed to .. if all of this falls into play then you can be
> as sured that you have a copy of your app running.

But there is no guarantee that any of that will fall into place.

When your main form is open in the IDE, it has the same class name and
title as the form your program creates. FindWindow might find that
window, which makes testing and debugging inconvenient. FindWindow is an
attractive function because it's easy to use and it's easy for novices
to understand. But it's simply not the right tool for the job.

> there the methods that others use like CreateMutex etc.. but i found
> that if the SENDMESSAGE works your app can return back aditional info
> that you may want also to insure its your app and not just some fluke
> that there is another app on the desktop running that way.,..

Using a mutex does not preclude usage of other techniques for
transferring control to the previous instance. However, a mutex is a
guaranteed method of detecting multiple instances. FindWindow offers
absolutely no guarantees.

Besides, if the window you find doesn't return the value you were
expecting, then you've determined that you didn't find the right window.
But now what? You still can't conclude that no other instances of your
program are running. FindWindow only returns one window, and it always
returns the first one it finds.

If you want to communicate with the existing instance of a program, then
you can use a mailslot instead of a mutex. Like mutex names, mailslot
names are unique on a system, so you can use CreateMailslot just as you
would use CreateMutex. Mailslots also allow you to send information to
the first instance, so the first instance can receive subsequent
instances' command-line parameters.

-- 
Rob


Relevant Pages

  • Re: Only One Instance.
    ... the secondary app the handle info of the original app along with what ... use the FINDWIndow in the Main Source before The ... The name of the mutex has no effect on anything else in your program, ... Creating a window, on the ...
    (comp.lang.pascal.delphi.misc)
  • Re: FindWindow is hanging....
    ... because any string you have depends on the fact that you are looking for a specific window ... in a specific version of the app, ... Since I don't trust FindWindow as far as I can throw a mainframe, ...
    (microsoft.public.vc.mfc)
  • Re: Pass in additional command line arguments to a single instance app
    ... Explorer to send the filename to my app. ... I use the existence of a window with a particular class name as the indicator that a "first instance" is running. ... An instance can not start (create the window), exit, or check for existence of the window unless it can acquire the mutex. ...
    (microsoft.public.vc.language)
  • RE: Single Application Instance Example
    ... All the Mutex is doing is providing a single token ... app and activate it (assuming it has a window). ... anti-virus service working around the clock, around the globe, visit: ...
    (comp.lang.python)
  • Re: passing multiple parameters to an application via windows explorer
    ... >> Thanks for the reply Jamie, but my problem isn't working out how to ... > main program via the createProcess, then a findWindow can be done ... > again to get the window handle of the main program. ... > to the window of your main app, ...
    (alt.comp.lang.borland-delphi)