Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Spawn process within console

Unscruffed

Programmer
Apr 2, 2002
102
0
16
AU
I have a little console app I wrote called FFMemMon.exe that monitors the memory used by ffmpeg.
I also have another little app called Wait.exe that simply waits x seconds before exiting.

When FFMemMon runs, it attaches to the console and stores the current cursor position. It then starts monitoring the process list and once it detects ffmpeg running, it starts monitoring that processes memory. Every second or so, it prints a status line in the console at the stored cursor location showing the current and peak memory usage for ffmpeg. This is done using the WriteConsoleOutput function.

For this to work, you need to use START /B FFMemMon so that the batch file keeps running - and that's where the problem is.

At the moment, I have a batch file that lloks like this:
Code:
@ECHO OFF
START /B FFMemMon
WAIT 0.5
ECHO.
ECHO.
FFMPEG ....

The problem is that the startup time for any app is never consistent and sometimes FFMemMon and ffmpeg end up writing their status lines to the same line in the console, when FFMemMon should be 2 lines above ffmpeg.

My hopeful solution is to do away with the whole START /B thing and have FFMemMon spawn itself (passing the cursor location on the command line) and then exit. The second instance would then be the one that runs in the background and printing the status line to the console.

So the batch file would then look like this:
Code:
@ECHO OFF
FFMemMon
ECHO.
ECHO.
FFMPEG ....
No need for START /B or WAIT.

So my question is: How do I start that second instance within the same console window from VB?
 
Last edited:
Thanks for the reply. I got it working.

Using CreateProcess to spawn the second instance worked - it attaches to the console no problem.

I also accidentally found out that when you compile with LinkSwitches=/SUBSYSTEM:CONSOLE, you don't need to call AttachConsole or AllocConsole - you can simply grab the STD handles and read/write to the console. I found that to be a bit strange, but no complaints here. :)
 
Worth pointing out for future reference that this is the VB 6 classic forum. No compiler switches available*. I rather suspected you might not be working in VB^ once you mentioned a Console application since, again, VB6 cannot create console apps*


* linker options, console apps and true DLLs can be created with Vb6 with some hacking of the (hidden) linker that the VB6 compiler uses in the background
 

Part and Inventory Search

Sponsor

Back
Top