View Single Post
Old 10-30-2006   #2 (permalink)
dreeschkind


 
 

RE: Run tasks asynchronously: external files vs. script blocks

"OK" wrote:

> I can run tasks asynchronously using external files, but can not do the same
> with script blocks...
>
> Running tasks asynchronously is the easy part. Gathering totals in one place
> is more difficult. Actually, it’s done already. But some questions are still
> there...


Can you be more clear on what is actually NOT working? What are the results
you are expecting and what are the results that you get?

Using your example with script files (#1), the output I'm getting is:
################
CommandText ./f2.ps1
IsScript False
UseLocalScope False
################

Using scriptblocks (#2), the output looks like:
################
CommandText param( [string] $p1) "hello world parameter1: $p1 input: $input"

IsScript True
UseLocalScope False
################

However, the following is a completely different approach on solving the
problem of timing asynchronously running tasks. Maybe this can help you with
your problem without raising more questions. ;-)

Note that escaping the scriptblock strings $sb1 and $sb2 is a little bit
weird, but it should be possible to replace this part with external script
files so that the new process will be executed as something like:
"powershell.exe -noprofile -command C:\script1.ps1"
Of course you can also directly specify external applications to be run.

################################
# start-jobdemo.ps1
################################
$scriptstarttime = get-date
"starting script: $scriptstarttime"
"-"*40

$sb1 = '&{''hello from ''+$pid; 0..10|%{''-'' + $_ + ''-''; sleep 1}}'
$sb2 = '&{''hello from ''+$pid; 0..20|%{''-'' + $_ + ''-''; sleep 1}}'

$externalWindow = $true

$StartInfo1 = new-object System.Diagnostics.ProcessStartInfo
$StartInfo1.FileName = "powershell.exe"
$StartInfo1.Arguments = "-noprofile -command $sb1"
$StartInfo1.UseShellExecute = $externalWindow
$p1 = [System.Diagnostics.Process]::Start($StartInfo1)
$p1starttime = get-date
"process1 started: $p1starttime"

$StartInfo2 = new-object System.Diagnostics.ProcessStartInfo
$StartInfo2.FileName = "powershell.exe"
$StartInfo2.Arguments = "-noprofile -command $sb2"
$StartInfo2.UseShellExecute = $externalWindow
$p2 = [System.Diagnostics.Process]::Start($StartInfo2)
$p2starttime = get-date
"process2 started: $p2starttime"

$p1finished = $false
$p2finished = $false

while (($p1finished -eq $false) -or ($p2finished -eq $false)) {
sleep 2
#"-"*40
#"p1 exited?: $($p1.hasexited) / $(get-date)"
if (($p1.hasexited -eq $true) -and ($p1finished -eq $false)) {
$p1endtime = get-date;
"process1 finished: $p1endtime"
$p1finished = $true
}
#"p2 exited?: $($p2.hasexited) / $(get-date)"
if (($p2.hasexited -eq $true) -and ($p2finished -eq $false)) {
$p2endtime = get-date;
"process2 finished: $p2endtime"
$p2finished = $true
}
}

"-"*40
$scriptendtime = get-date
"script finished: $scriptendtime"
"-"*40
"process1 total time: $($p1endtime - $p1starttime)"
"process2 total time: $($p2endtime - $p2starttime)"
"-"*40
"script total time: $($scriptendtime - $scriptstarttime)"
################################


--
greetings
dreeschkind
My System SpecsSystem Spec