From: David Craig on
Maybe the 'temporary files' can be opened in a way that permits you
determine if another instance has them opened. I think that all should use
sharing of reads and writes when the files are being used and they should
not be closed until the instance wants to exit. Then, at exit, open them
exclusive and if you obtain them, then they can be deleted by making the
open a 'delete on close' type open that last exclusive time. There may be a
'window of opportunity' where a starting instance might see the files and
then they are deleted before they open them, but that can be fixed by making
the opens the real sharing open. The return values from CreateFile() will
show you if the files already existed or were newly created.

"Allan Phillips" <allanephillips(a)gmail.com> wrote in message
news:ulsqD#brKHA.4492(a)TK2MSFTNGP05.phx.gbl...
> 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
>>
>>
>
>
From: David Lowndes 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?

Yes.

Any Win32 application can use the following technique:

BOOL bFound = FALSE;

hMutexOneInstance = CreateMutex( NULL, TRUE,
_T("YourUniqueGuidHere") );

if(GetLastError() == ERROR_ALREADY_EXISTS)
bFound = TRUE;

if(hMutexOneInstance)
ReleaseMutex(hMutexOneInstance);

If you want to have the scope of the name global for the machine
rather than just the window station, prefix the mutex name with
"Global\" - see the help on CreateMutex for more information.

If you want to actually count the number of instances, use a semaphore
rather than a mutex.

Dave
From: m on
AFAIK, the only reliable way to detect the close of the last instance of an
application is to do it out-of-band in another app (i.e. a watchdog
service). But for your problem of cleaning up shared temp files, the only
reliable implementation will have each instance of the app call CreateFile
with no access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_READ &
FILE_FLAG_DELETE_ON_CLOSE and not close this handle until it is about to
exit. This design will use the OS's kernel object reference count mechanism
to clean up the file(s) when there are no more instances of your app running
without relying on a period of quiescence between last instance shutdown a
new instance start up, and without any possibility of 'leaking' the files on
a crash.

Additionally, since the call to CreateFile would request no access and share
for all access, you can use your existing temporary file access code,
including calls to CreateFile & CloseHandle, with no changes since the
handle opened on start-up will prevent the file from being deleted until the
last handle (the last instance of your app in any session) is closed and won't
restrict the exiting logic from opening & closing the file any number of
times in the meantime.

"David Craig" <drivers(a)noemail.noemail> wrote in message
news:#2vTOdcrKHA.4828(a)TK2MSFTNGP05.phx.gbl...
> Maybe the 'temporary files' can be opened in a way that permits you
> determine if another instance has them opened. I think that all should
> use sharing of reads and writes when the files are being used and they
> should not be closed until the instance wants to exit. Then, at exit,
> open them exclusive and if you obtain them, then they can be deleted by
> making the open a 'delete on close' type open that last exclusive time.
> There may be a 'window of opportunity' where a starting instance might see
> the files and then they are deleted before they open them, but that can be
> fixed by making the opens the real sharing open. The return values from
> CreateFile() will show you if the files already existed or were newly
> created.
>
> "Allan Phillips" <allanephillips(a)gmail.com> wrote in message
> news:ulsqD#brKHA.4492(a)TK2MSFTNGP05.phx.gbl...
>> 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
>>>
>>>
>>
>>
From: Hector Santos on
Wouldn't opening up a file in exclusive mode be better or work just as
well? Secondary instances will fail. This is what we do with our
server.

On Feb 14, 10:50 pm, "m" <m...(a)b.c> wrote:
> AFAIK, the only reliable way to detect the close of the last instance of an
> application is to do it out-of-band in another app (i.e. a watchdog
> service). But for your problem of cleaning up shared temp files, the only
> reliable implementation will have each instance of the app call CreateFile
> with no access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_READ &
> FILE_FLAG_DELETE_ON_CLOSE and not close this handle until it is about to
> exit. This design will use the OS's kernel object reference count mechanism
> to clean up the file(s) when there are no more instances of your app running
> without relying on a period of quiescence between last instance shutdown a
> new instance start up, and without any possibility of 'leaking' the files on
> a crash.
>
> Additionally, since the call to CreateFile would request no access and share
> for all access, you can use your existing temporary file access code,
> including calls to CreateFile & CloseHandle, with no changes since the
> handle opened on start-up will prevent the file from being deleted until the
> last handle (the last instance of your app in any session) is closed and won't
> restrict the exiting logic from opening & closing the file any number of
> times in the meantime.
>
> "David Craig" <driv...(a)noemail.noemail> wrote in message
>
> news:#2vTOdcrKHA.4828(a)TK2MSFTNGP05.phx.gbl...
>
> > Maybe the 'temporary files' can be opened in a way that permits you
> > determine if another instance has them opened. I think that all should
> > use sharing of reads and writes when the files are being used and they
> > should not be closed until the instance wants to exit. Then, at exit,
> > open them exclusive and if you obtain them, then they can be deleted by
> > making the open a 'delete on close' type open that last exclusive time.
> > There may be a 'window of opportunity' where a starting instance might see
> > the files and then they are deleted before they open them, but that can be
> > fixed by making the opens the real sharing open. The return values from
> > CreateFile() will show you if the files already existed or were newly
> > created.
>
> > "Allan Phillips" <allanephill...(a)gmail.com> wrote in message
> >news:ulsqD#brKHA.4492(a)TK2MSFTNGP05.phx.gbl...
> >> 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" <kustt...(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

From: Allan Phillips on
Thanks a lot Dave.

This worked just fine.

Allan.

"David Lowndes" <DavidL(a)example.invalid> wrote in message
news:gd5hn5583phk2oqvt79i3ds077ft7snofn(a)4ax.com...
> >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?
>
> Yes.
>
> Any Win32 application can use the following technique:
>
> BOOL bFound = FALSE;
>
> hMutexOneInstance = CreateMutex( NULL, TRUE,
> _T("YourUniqueGuidHere") );
>
> if(GetLastError() == ERROR_ALREADY_EXISTS)
> bFound = TRUE;
>
> if(hMutexOneInstance)
> ReleaseMutex(hMutexOneInstance);
>
> If you want to have the scope of the name global for the machine
> rather than just the window station, prefix the mutex name with
> "Global\" - see the help on CreateMutex for more information.
>
> If you want to actually count the number of instances, use a semaphore
> rather than a mutex.
>
> Dave