Reputation: 470
In my TCL script I am running a process which can be rerun after the test completes. However, there is a 40 second wait period to allow enough time for the program to start up. If the program is running I do not want to have this 40 second waiting period. Is there anyway to search and read the tasklist to determine if the program is currently running? I was thinking about the PID but it changes everytime I run the program probably because it's being placed in a different area of memory.
I have a batch file written that do this process but then I would need to pass the result back into TCL. Is there any way this can be accomplished?
Upvotes: 1
Views: 1371
Reputation: 15163
Yes, it is possible, but you need a Tcl extension: Twapi.
To check if a process is running, you could try to find a process with a specific name. you could use the this:
package require twapi
if {[llength [twapi::get_process_ids -name YOUR_EXECUTABLE.EXE]]} {
puts "Process is currently running"
} else {
puts "Process not running"
}
See the manual about twapi::get_process_ids for more information.
If you need a notification when a process starts/stops, it will be trickier and I have to look it up.
Upvotes: 1
Reputation: 493
in the special case, where you have the chance, to read from the started program (or write to the program if that reads from its stdin) you can start the process with open
set descriptor [open "|myprogram" r]
and then detect with 'eof' when the pipe closes (that implies that you must read the data from this channel e.g. using 'read')
May be you can write a wrapper around your program. This wrapper can send to stdout and allows the invoking tcl-Script to detect the end of the program.
Upvotes: 0
Reputation: 40688
If you don't have twapi, here is an alternative: use tasklist.exe
:
package require csv
proc getPids {imageName} {
set noTaskMessage "INFO: No tasks are running which match the specified criteria."
set output [exec tasklist.exe /fi "imagename eq $imageName" /fo csv /nh]
set pidList {}
if {$output != $noTaskMessage} {
foreach line [split $output \n] {
set tokens [::csv::split $line]
lappend pidList [lindex $tokens 1]
}
}
return $pidList
}
# Try it out
set imageName chrome.exe
set pids [getPids $imageName]
if {$pids == ""} {
puts "Process $imageName is not running"
} else {
puts "PIDs for $imageName: $pids"
}
/fi
flag specifies a filter, in this case, we want to filter by image name/fo
flag specifies the output format: TABLE (default), LIST, and CSV. I choose CSV because it is the easiest to parse./nh
tells tasklist.exe to omit the header (AKA no header).getPids
returns an empty list, the process is not running.getPids
might return more than one PIDs, so you code must be able to handle that case.Upvotes: 3