From: m on
I understood that the OP wanted multiple instances to run, but was looking
for a reliable way to cleanup some shared temp files?

"Hector Santos" <sant9442(a)gmail.com> wrote in message
news:6bb961dc-ce54-4934-9696-ec999c7be186(a)j27g2000yqn.googlegroups.com...
> 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
>