如何将此Powershell cmdlet包装到超时函数中?

时间:2019-05-11 05:51:35

标签: multithreading powershell asynchronous delegates task

我已经尝试过(但失败了)让[System.Delegate] :: CreateDelegate(工作,新对象[System.Threading.Tasks.Task] :: Run(工作,通过Wait开始作业-Job,似乎没有设置Powershell来执行带有等待超时的异步任务吗?

如果有人有任何想法,那将很棒。

do 
{
    Sleep 2;
    $testShare = Test-Path "\\$server\c$";
    Write-Host "Share availability is:" $testShare;
}while($testShare -eq $true) # wait for the share to become unavailable

亚当斯的建议

do 
{
    Sleep 1;
    #$testShare = Test-Path "\\$server\c$"; # could use code here to deal with the hanging

    $timeout_in_seconds = 5;
    $timer = [Diagnostics.Stopwatch]::StartNew();
    do 
    {
        Write-Host "    Test-path inside of second do loop";
        Start-Sleep -Seconds 1;
        $testShare = Test-Path "\\$server\c$";
        Write-Host "    (Inner loop) Share availability is:" $testShare;
    } while ( (1 -eq 1) -and ($timer.Elapsed.TotalSeconds -lt $timeout_in_seconds) )
    $timer.Stop();
    $timer.Elapsed.TotalSeconds;

    Write-Host "";
    Write-Host "(Outer loop) Share availability is:" $testShare;
} while($testShare -eq $true) # wait for the share to become unavailable

输出

    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
5.3015436

(Outer loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
5.2303907

(Outer loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: True
    Test-path inside of second do loop
    (Inner loop) Share availability is: False
**42.1773323**

(Outer loop) Share availability is: False
Ping availability is: False

1 个答案:

答案 0 :(得分:2)

几件事...

发生超时并不需要异步处理。您可能具有一个超时的同步过程(如您的示例)。

带有超时时间的简单同步脚本...

$timeout_in_seconds = 10
$timer = [Diagnostics.Stopwatch]::StartNew()

do {
    Start-Sleep -Seconds 1
    Write-Host 'Doing stuff'

} while ( (1 -eq 1) -and ($timer.Elapsed.TotalSeconds -lt $timeout_in_seconds) )

$timer.Stop()
$timer.Elapsed.TotalSeconds

简化示例只是为了说明这一点。我正在设置运行间隔(10秒)。我正在启动一个计时器。我运行一个循环,直到遇到成功条件(在此示例中,我永远不会成功)或达到超时。您会做同样的事情。

对于您的特定示例,请考虑类似...

$server = '...'
$timeout_in_seconds = 5;
$timer = [Diagnostics.Stopwatch]::StartNew();

do {
    Write-Host "Test-path inside of loop";
    Start-Sleep -Seconds 1;
    $testShare = Test-Path "\\$server\c$";
    Write-Host "Share availability is:" $testShare;
} while ( $($testShare) -and ($timer.Elapsed.TotalSeconds -lt $timeout_in_seconds) )

$timer.Stop();
$timer.Elapsed.TotalSeconds;

一旦共享存在或达到时间间隔,循环就会终止。请注意,您需要设置$server变量。