General discussion

Locked

Running an exe in VB

By bcollins ·
I am writing a VB program that requires a call to run an exe file. This exe file is producing a txt file that I then need to get some information out of. My problem however is that the VB program makes a call to the program (using Shell() ) and then jumps right into trying to get the information out of the txt file. Is there anyway I can get the VB program to wait until the exe file has finished running?

This conversation is currently closed to new comments.

13 total posts (Page 1 of 2)   01 | 02   Next
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Running an exe in VB

by ravikishore_g In reply to Running an exe in VB

please use the sendkeys statement and send some character other than control char, immediately after the usage of shell function. while doing so set the wait parameter to true so the control returns to your procedure(next statement ) only after the key is processed. this is a work around for your problem and may not be useful if the program being called takes user input refer to online help

Collapse -

Running an exe in VB

by bcollins In reply to Running an exe in VB

The question was auto-closed by TechRepublic

Collapse -

Running an exe in VB

by tclere In reply to Running an exe in VB

sTxtFile = "C:\path & filename.txt)
sExeFile = "C:\path & application.ext)

'Remove old copy of txt file
If Dir(sTxtFile)<>"" then Kill (sTxtFile)

'Run the exe file
Call Shell(sExeFile, vbNormalFocus)

'Wait until file is written to continue
Do While Dir(sTxtFile) = ""
DoEvents
Loop

If the txt file is long, the Dir(sTxtFile) command may see the file before it is completed. You could add a few seconds of a delay if this occurs. If you have access to rewrite the exe being called, you could also write a tmp file before the Shell() command and remove the tmp file as the last step in the sExeFile. I have a routine (somewhat long) that will check if the sExeFile is running in Task Manager. Let me know if you want me to send you this code.

Collapse -

Running an exe in VB

by bcollins In reply to Running an exe in VB

The question was auto-closed by TechRepublic

Collapse -

Running an exe in VB

by Lo In reply to Running an exe in VB

Another workaround - Try to open the text file in exclusive I/O mode. You should get an error return until it is close by the other program. In conjunction with Dirs to make sure it is there to start, wait and a loop, that should work.

Yet one more - if you have access to the Run program, add a 'last line'. If the file isn't too large, open, read discard until you the the 'last line' record.

BTW - neither of these are very efficient in system usage, but ...

Collapse -

Running an exe in VB

by bcollins In reply to Running an exe in VB

The question was auto-closed by TechRepublic

Collapse -

Running an exe in VB

by bogdincescu In reply to Running an exe in VB

I believe that if you could make a slight change to the .exe that generates your text file so that, after generating the needed text file it would write a little something to a file, say done.log, then, your VB program could loop until it finds the file done.log, then start reading the text file.
If you can't change the .exe file (say, because you can't obtain the sources), you may use a batch like the following:

-------
@echo off
del done.log
prepare_stuff.exe
echo done>done.log
------
So, your VB program would call the .bat and start using the generated text file only after it finds the done.log file. Yet, it certainly would be the best to have the done.log generated through your .exe file, for, if something goes wrong with your .exe and it dies for some reason, then your VB program won't use incomplete data.

Collapse -

Running an exe in VB

by bcollins In reply to Running an exe in VB

The question was auto-closed by TechRepublic

Collapse -

Running an exe in VB

by reassembler In reply to Running an exe in VB

The Windows API has functions that exist for the sole purpose of starting programs and waiting for programs to finish.

You get a handle to the process you want to wait on by call CreateProcess.

Use the handle in call to WaitForSingleObject.When WaitFOrSingleObject returns, the process is dead.

Collapse -

Running an exe in VB

by bcollins In reply to Running an exe in VB

The question was auto-closed by TechRepublic

Back to Web Development Forum
13 total posts (Page 1 of 2)   01 | 02   Next

Related Discussions

Related Forums