2013-07-17 23 views
0

我在網上找到了一個代碼,一個函數的工作代碼可以使文本閃爍。我在我的pwershell應用程序中使用該函數。但我想讓閃爍的代碼在後臺運行。 功能是如何在後臺運行閃爍的文本代碼

$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 

任何意見讚賞。

謝謝。

回答

0

這似乎工作正常。比我想象的要好(基本上有兩個線程同時寫入控制檯,並且它們不同步,這通常是不可預知的行爲的配方)。

我添加了$ x和$ y參數。 $ x是您希望閃爍的消息所在的列。$ y是當前提示所在行上方的行數。如果你使$ y小於$host.ui.rowui.windowsize.height,看起來最好。如果你這樣做,閃爍的消息在屏幕的頂部。否則,只要控制檯滾動,閃爍的消息就會在多行上結束。請注意,此腳本僅在POWERSHELL.EXE上進行測試。它絕對不能在powershell_ise上運行。

在寫這篇文章時,我崩潰/掛了很多次powershell。每次運行腳本時,它都會創建一個將保持「正在運行」狀態的作業(即使在完成所有由$ count指定的重複之後)。讓多個作業在後臺運行,可能所有寫入屏幕的內容可能都是你想要避免的。 jobless函數將刪除所有作業。

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 to kill all jobs 
function jobless { get-job;get-job|Stop-Job -pass | Remove-Job}