Detecting a file close event
-
Greetings, I have a program in which I spawn a process (AcroRd32.exe with the proper parameters) and I want to wait until it has finished writing a file and then close the process. (I am "printing" to a file.) I know that I can poll the file's size and wait for it to stop growing for some arbitrary time, but this is unsatisfactory for obvious reasons. It seems like the OS knows when a file gets opened and when the file handle is closed. Is there an easy way, given the full pathname to the file, to detect when a file handle gets closed? I vaguely recall that there was some Win32 API that might help do this but I cannot find it. I really have searched for it, but I guess I'm not using the right keywords! Thanks for any pointers. -- Harold
-
Greetings, I have a program in which I spawn a process (AcroRd32.exe with the proper parameters) and I want to wait until it has finished writing a file and then close the process. (I am "printing" to a file.) I know that I can poll the file's size and wait for it to stop growing for some arbitrary time, but this is unsatisfactory for obvious reasons. It seems like the OS knows when a file gets opened and when the file handle is closed. Is there an easy way, given the full pathname to the file, to detect when a file handle gets closed? I vaguely recall that there was some Win32 API that might help do this but I cannot find it. I really have searched for it, but I guess I'm not using the right keywords! Thanks for any pointers. -- Harold
Why not just wait for the spawned process to finish? You can do this with CreateProcess() and WaitForSingleObject(). If you want specific code let know. Neville Franks, Author of ED for Windows. www.getsoft.com
-
Why not just wait for the spawned process to finish? You can do this with CreateProcess() and WaitForSingleObject(). If you want specific code let know. Neville Franks, Author of ED for Windows. www.getsoft.com
Oh, would that it were so easy! :laugh: I am spawning something like:
AcroRd32.exe /n /t "PSPrinter" "HP LaserJet 5000 Series PS" "filexyz.pdf" "filexyz.ps"
to print a PDF file to a Postscript file. Unfortunately, AcroRd32.exe doesn't exit after doing this. It just sits around, using up resources. And removing the
/n
doesn't help. Sigh... But thanks for the suggestion. Ordinarily that would be the clear winner. -
Oh, would that it were so easy! :laugh: I am spawning something like:
AcroRd32.exe /n /t "PSPrinter" "HP LaserJet 5000 Series PS" "filexyz.pdf" "filexyz.ps"
to print a PDF file to a Postscript file. Unfortunately, AcroRd32.exe doesn't exit after doing this. It just sits around, using up resources. And removing the
/n
doesn't help. Sigh... But thanks for the suggestion. Ordinarily that would be the clear winner.Possibly there's a neater way using some functions from the Shell library, but the following hack should work: Keep trying to open
filexyz.ps
in non-create, write mode periodically until you succeed. Then you knowAcroRd32.exe
is done. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
Possibly there's a neater way using some functions from the Shell library, but the following hack should work: Keep trying to open
filexyz.ps
in non-create, write mode periodically until you succeed. Then you knowAcroRd32.exe
is done. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloSounds promising, assuming that AcroRd32.exe normally locks its output file (or the OS does this automatically). I'll give that a try and report back (tomorrow, probably). Thanks!
-
Oh, would that it were so easy! :laugh: I am spawning something like:
AcroRd32.exe /n /t "PSPrinter" "HP LaserJet 5000 Series PS" "filexyz.pdf" "filexyz.ps"
to print a PDF file to a Postscript file. Unfortunately, AcroRd32.exe doesn't exit after doing this. It just sits around, using up resources. And removing the
/n
doesn't help. Sigh... But thanks for the suggestion. Ordinarily that would be the clear winner.Harold Bamford wrote: Oh, would that it were so easy! Yep.;) I'd like to think AcroRd32.exe locked the file it was printing to. If it does then you could try opening it for write access from time to time and when the open succeeded you'd be away. There is a Win API FindFirstChangeNotification() which would be worth looking at, but in this case I don't think it will help. See: http://www.codeproject.com/file/filewatch.asp[^]etc. Also have a look at www.sysinternals.com (seems to be down at present). They have a program called called something like FileWatch which should provide a solution. Neville Franks, Author of ED for Windows. www.getsoft.com
-
Harold Bamford wrote: Oh, would that it were so easy! Yep.;) I'd like to think AcroRd32.exe locked the file it was printing to. If it does then you could try opening it for write access from time to time and when the open succeeded you'd be away. There is a Win API FindFirstChangeNotification() which would be worth looking at, but in this case I don't think it will help. See: http://www.codeproject.com/file/filewatch.asp[^]etc. Also have a look at www.sysinternals.com (seems to be down at present). They have a program called called something like FileWatch which should provide a solution. Neville Franks, Author of ED for Windows. www.getsoft.com
Well, I tried it and the method suggested by you and Joaquín M López Muñoz worked perfectly. On the off-chance that AcroRd32.exe creates an empty file before writing to it, I added a check for a non-zero length file, but basically the technique works. Thanks to both of you!