Cae*_*sar 27 powershell multithreading
我想在PowerShell中将一些文件解析操作与网络活动并行化.快速google for it,start-thread看起来像一个解决方案,但是:
术语"start-thread"不被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.
当我尝试开始工作时,同样的事情发生了.
我也试过摆弄System.Threading.Thread
[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
#This next errors, something about the arguments I can't figure out from the documentation of .NET
$tstart = new-object System.Threading.ThreadStart({DoSomething})
$thread = new-object System.Threading.Thread($tstart)
$thread.Start()
Run Code Online (Sandbox Code Playgroud)
所以,我认为最好的方法是在使用start-thread时知道我做错了什么,因为它似乎适用于其他人.我使用v2.0,我不需要向下兼容.
dri*_*iis 32
Powershell没有名为Start-Thread的内置命令.
但是,V2.0具有PowerShell作业,可以在后台运行,并且可以被认为是线程的等效物.您可以使用以下命令来处理作业:
Name Category Synopsis
---- -------- --------
Start-Job Cmdlet Starts a Windows PowerShell background job.
Get-Job Cmdlet Gets Windows PowerShell background jobs that are running in the current ...
Receive-Job Cmdlet Gets the results of the Windows PowerShell background jobs in the curren...
Stop-Job Cmdlet Stops a Windows PowerShell background job.
Wait-Job Cmdlet Suppresses the command prompt until one or all of the Windows PowerShell...
Remove-Job Cmdlet Deletes a Windows PowerShell background job.
Run Code Online (Sandbox Code Playgroud)
这是一个如何使用它的示例.要启动作业,请使用start-job并传递一个脚本块,其中包含要异步运行的代码:
$job = start-job { get-childitem . -recurse }
Run Code Online (Sandbox Code Playgroud)
此命令将启动一个作业,以递归方式获取当前目录下的所有子项,并且您将立即返回到命令行.
您可以检查$job变量以查看作业是否已完成等.如果要等待作业完成,请使用:
wait-job $job
Run Code Online (Sandbox Code Playgroud)
最后,要接收作业的结果,请使用:
receive-job $job
Run Code Online (Sandbox Code Playgroud)
x0n*_*x0n 10
你不能像这样直接使用线程,但你不能因为尝试而被责备,因为一旦整个BCL躺在你面前,期待大部分工作都不是完全愚蠢的:)
PowerShell在管道中运行脚本块,而脚本块又需要运行空间来执行它们.我在博客上写了关于如何为v2 ctp3推出自己的MT脚本,但技术(和API)仍然是相同的.主要工具是[runspacefactory]和[powershell]类型.看看这里:
http://www.nivot.org/2009/01/22/CTP3TheRunspaceFactoryAndPowerShellAccelerators.aspx
以上是处理MT脚本的最轻量级方法.通过启动工作,获取工作在v2中有后台工作支持,但我认为你已经发现了这一点,并且发现它们是相当重量级的.
| 归档时间: |
|
| 查看次数: |
40674 次 |
| 最近记录: |