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

任何建议表示赞赏。

谢谢。

4

1 回答 1

0

这似乎工作正常。比我想象的要好(基本上有两个线程同时写入控制台,并且它们不同步,这通常是导致不可预测行为的原因)。

我添加了 $x 和 $y 参数。$x 是您希望闪烁消息所在的列。 $y 是当前提示所在行上方的行数。如果你使 $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}
于 2013-07-18T00:47:48.643 回答