Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么可以';PowerShell创建的线程是否执行脚本函数?_Powershell_Start Job_Createthread - Fatal编程技术网

为什么可以';PowerShell创建的线程是否执行脚本函数?

为什么可以';PowerShell创建的线程是否执行脚本函数?,powershell,start-job,createthread,Powershell,Start Job,Createthread,我有一个脚本函数调用。net操作word文档。它起作用了。现在我想创建一个子线程来执行它,然后主线程决定它是完成还是超过指定的时间,并在该时间之后结束它。 如代码所示,它不执行$node代码块中的函数,而是$task1执行cmdlet。为什么呢?我怎样才能满足我的需求 try{ # $cb is a instance of class,scan is the function I want to invoke. $code = { $cb.Scan($PrepareFileName, $

我有一个脚本函数调用。net操作word文档。它起作用了。现在我想创建一个子线程来执行它,然后主线程决定它是完成还是超过指定的时间,并在该时间之后结束它。 如代码所示,它不执行$node代码块中的函数,而是$task1执行cmdlet。为什么呢?我怎样才能满足我的需求

try{
# $cb is a instance of class,scan is the function I want to invoke.
    $code = { $cb.Scan($PrepareFileName, $NailDirName, $HtmlFileName) }
#    $task1 = { Start-Sleep -Seconds 9; Get-Service }
    $newThread = [PowerShell]::Create().AddScript($code)
    $handleTh = $newThread.BeginInvoke()
    $nTimes = 0;
    do
    {
        $nTimes++;
        if($handleTh.IsCompleted -or $nTimes -gt 10)
        {
          break;  
        }
        Start-Sleep -Milliseconds 500

    } while($true)

    $newThread.EndInvoke($handleTh)
    $newThread.Runspace.Close()
    $newThread.Dispose()

}catch{

}

您需要创建一个
运行空间
,并将其保存到PowerShell对象。检查本“教程”,了解如何正确使用运行空间。还解释了如何使用运行空间池和脚本块参数

try{
    # $cb is a instance of class,scan is the function I want to invoke.
    $code = { 
        # Update 1, added parameter
        param($cb)
        $cb.Scan($PrepareFileName, $NailDirName, $HtmlFileName) 
    }
    # Create a runspace
    $runspace = [runspacefactory]::CreateRunspace()
    # Update 1, inject parameter
    $newThread = [PowerShell]::Create().AddScript($code).AddParameter(‘cb’,$callback)

    # Add the runspace
    $newThread.Runspace = $runspace
    $runspace.Open()
    $handleTh = $newThread.BeginInvoke()
    $nTimes = 0;
    do
    {
        $nTimes++;
        if($handleTh.IsCompleted -or $nTimes -gt 10)
        {
          break;  
        }
        Start-Sleep -Milliseconds 500

    } while($true)

    $newThread.EndInvoke($handleTh)
    $newThread.Dispose()
}
catch{
}
希望有帮助


新线程从哪里开始?这不是句子吗
$handleTh=$newThread.BeginInvoke()
@rossbushNewPowerShell的
运行空间
,您在当前
运行空间
中创建的,对变量一无所知。我认为您是对的。对于新的PowerShell,有什么好方法可以从父级运行时获取变量吗@佩瑟拉尔@不知火舞 尝试使用PosshRSJob,它允许您在不同的运行空间中处理powershell作业,并允许您从“父范围”访问变量。谢谢你的回答。我现在也在做同样的事情。但同样的结果是:$handleTh。IsCompleted始终等于$true@MoerwaldIt表示$handleTh.IsCompleted的结果在进入扫描功能之前为$true@MoerwaldI做了个测试。我将
$cb.Scan($PrepareFileName、$NailDirName、$HtmlFileName)
替换为
开始睡眠-30秒
。在睡眠期间,
$handleTh.IsCompleted
false
。是
$cb
$null
还是
Scan
方法返回错误?将
$cb.Scan($PrepareFileName,$NailDirName,$HtmlFileName)
替换为
开始睡眠-30秒
。现在是
$handleTh
false
?这是我的问题。执行cmdlet没有问题,但执行函数不会起作用。不通过[PowerShell]:Create()执行此函数完全可以。