0

This question is similar to most about ShellExecute, with one exception: I want to use ShellExecute because it's simple to use and it can be called with a few lines from within many programming languages including the now sunsetted Visual FoxPro.

Most existing solutions rely on doing stuff with threads, looking for processes (not reliable if it's a common process name) or ShellExecuteEx which is much more complicated to use. I was not satisfied with other solutions out there.

The issue is that I have a file operation that takes a few seconds to complete: unzipping an archive. How do I know that I am done?

Jacob Bruinsma
  • 1,087
  • 1
  • 10
  • 23

1 Answers1

0

The solution is to create a semaphore from within the command being executed. This can be done by running a batch file, but if that is not desired, there is another way to handle it with a one-liner using cmd.exe:

ShellExecute(0, 'open', 'cmd.exe', '/c 7z.exe e Invoices.zip | find "Everything is Ok" > semaphore.txt', workingDirectory, 1)

This one-liner uses CMD to pipe the output of 7zip to Find, which looks for the line of text that indicates completion. Of course, this fails if there's an error.

If you don't have output you to wait for (for piping into FIND), you can use & echo thusly:

... & echo > semaphore.txt

This waits until ... is done, then echos a newline to semaphore.txt.

At the end, you'll have a semaphore.txt file inside your working directory. All you have to do is look for this file to appear, and you know that execution is done. Delete this file and continue with your program.

Jacob Bruinsma
  • 1,087
  • 1
  • 10
  • 23