From: Allan Phillips on
I have a Win32 MFC MDI app and from within that app I would like to
determine how many instances of that app are currently running. What I
really need to know is whether this is the only instance of the application
running.

Is there a way to establish that?



From: Stefan Kuhr on
Hi Allan,

On 2/14/2010 8:06 PM, Allan Phillips wrote:
> I have a Win32 MFC MDI app and from within that app I would like to
> determine how many instances of that app are currently running. What I
> really need to know is whether this is the only instance of the application
> running.
>
> Is there a way to establish that?
>
>
>

Typically you use a window class of your own and do a FindWindow on that
windows class followed by CreateWindow if that window class cannot be
found, all wrapped by entering a critical section implemented by a named
mutex with a name of your choosing that should be unique on the terminal
session. This way you can guarantee, that you have only instance of your
app on each terminal session. Is this (having only one instance of your
app per terminal session) what you want to achieve?

--
S
From: Allan Phillips on
It's an MFC app so the window class seems to get established via some MFC
magic.

Actually, when my instance is exiting I need to know if it's the last
instance running.


"Stefan Kuhr" <kustt110(a)gmx.li> wrote in message
news:%23XCj$parKHA.4492(a)TK2MSFTNGP05.phx.gbl...
> Hi Allan,
>
> On 2/14/2010 8:06 PM, Allan Phillips wrote:
>> I have a Win32 MFC MDI app and from within that app I would like to
>> determine how many instances of that app are currently running. What I
>> really need to know is whether this is the only instance of the
>> application
>> running.
>>
>> Is there a way to establish that?
>>
>>
>>
>
> Typically you use a window class of your own and do a FindWindow on that
> windows class followed by CreateWindow if that window class cannot be
> found, all wrapped by entering a critical section implemented by a named
> mutex with a name of your choosing that should be unique on the terminal
> session. This way you can guarantee, that you have only instance of your
> app on each terminal session. Is this (having only one instance of your
> app per terminal session) what you want to achieve?
>
> --
> S


From: Stefan Kuhr on
Hi Allan,

On 2/14/2010 8:27 PM, Allan Phillips wrote:
> It's an MFC app so the window class seems to get established via some MFC
> magic.
>
> Actually, when my instance is exiting I need to know if it's the last
> instance running.
>

You can register your own window class and use it with MFC classes,
there is no magic behind that. I see you have some unusual requirement,
because you need to know when your last instance exits. You might want
to use a DWORD as shared memory between instances. Increment that DWORD
with each instance start and decrement it with the end of each instance.
However, you might get into trouble if one of your instances crashes and
cannot decrement this value. I cannot think of a real easy solution to
this problem. What is it that you really want to achieve, what should
the last instance of your app doing when it terminates?

--
S


From: Allan Phillips on
Stefan,

It's actually doing some temporary file cleanup so it's not real crucial
that it happens. However, it's important that it doesn't happen too early
(when another instance is still running).

Allan.

"Stefan Kuhr" <kustt110(a)gmx.li> wrote in message
news:%23Qa1y4arKHA.3408(a)TK2MSFTNGP06.phx.gbl...
> Hi Allan,
>
> On 2/14/2010 8:27 PM, Allan Phillips wrote:
>> It's an MFC app so the window class seems to get established via some MFC
>> magic.
>>
>> Actually, when my instance is exiting I need to know if it's the last
>> instance running.
>>
>
> You can register your own window class and use it with MFC classes, there
> is no magic behind that. I see you have some unusual requirement, because
> you need to know when your last instance exits. You might want to use a
> DWORD as shared memory between instances. Increment that DWORD with each
> instance start and decrement it with the end of each instance. However,
> you might get into trouble if one of your instances crashes and cannot
> decrement this value. I cannot think of a real easy solution to this
> problem. What is it that you really want to achieve, what should the last
> instance of your app doing when it terminates?
>
> --
> S
>
>