powershell - How to run a blinking text code in background -
i found code on internet ,working code of function blink text. using function in pwershell application . want blinking code run in background. function
$function = { function blink-message { param([string]$message,[int]$delay,[int]$count,[consolecolor[]]$colors) $startcolor = [console]::foregroundcolor $startleft = [console]::cursorleft $starttop = [console]::cursortop $colorcount = $colors.length $line = "$message" for($i = 0; $i -lt $count; $i++) { [console]::cursorleft = $startleft [console]::cursortop = $starttop [console]::foregroundcolor = $colors[$($i % $colorcount)] [console]::writeline($message) start-sleep -milliseconds $delay } [console]::foregroundcolor = $startcolor } } # set-alias blink blink-message #write-host -nonewline "hello "; blink-message "blink" 1000 15 "red,black" | receive-job write-host -nonewline "hello1 "; start-job -initializationscript $function -scriptblock {blink-message} -inputobject "blink1",1000,15,"red,black" | receive-job write-host -nonewline "hello2 "; start-job -initializationscript $function -scriptblock {blink-message} -inputobject "blink2",1000,15,"red,black" | receive-job write-host -nonewline "hello3 "; start-job -initializationscript $function -scriptblock {blink-message} -inputobject "blink3",1000,15,"red,black" | receive-job
any advice appreciated.
thanks.
this seems work ok. better thought (there 2 threads writing console @ same time , aren't synchronized recipe unpredictable behavior).
i added $x , $y parameters. $x column want blinking message in. $y number of rows above row prompt is. looks best if make $y 1 less $host.ui.rowui.windowsize.height
flashing message @ top of screen if do. otherwise flashing message end on multiple lines whenever console scrolls. note script tested on powershell.exe. not work on powershell_ise.
while writing this, crashed/hung powershell number of times. each time run script creates job remain in "running" state (even after repetitions specified $count have completed). having multiple jobs running in background, potentially writing screen want avoid. jobless
function remove jobs.
function blink-message { param([string]$message,[int]$delay,[int]$count,[consolecolor[]]$colors,$x=0,$y=0) $script:colorcount = $colors.length $script:thecolors = $colors $script:thecount = $count $script:msg = $message $script:col = $x $script:deltarow = $y $tmr = new-object timers.timer $delay $tmr.autoreset = $true $tmr.enabled = $true $script:cnt = 0 $null = register-objectevent $tmr -eventname elapsed -action { param( $sender) $curcolor = [console]::foregroundcolor $curleft = [console]::cursorleft $curtop = [console]::cursortop [console]::cursorleft = $col if ([console]::cursortop -lt -$deltarow) { [console]::cursortop = 0 } else { [console]::cursortop += $deltarow } [console]::foregroundcolor = $thecolors[$($cnt % $colorcount)] [console]::writeline($msg) [console]::cursorleft = $curleft [console]::cursortop = $curtop [console]::foregroundcolor = $curcolor if ($script:cnt++ -ge $thecount) { try {$sender.enabled=$false} catch{wh send catch} } } } set-alias blink blink-message blink "$("="*30) gabby foo" 100 100 "green","black","yellow","darkblue" 0 -79 # # use jobless kill jobs function jobless { get-job;get-job|stop-job -pass | remove-job}
Comments
Post a Comment