Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does threading in powershell work?

I want to parallelize some file-parsing actions with network activity in powershell. Quick google for it, start-thread looked like a solution, but:

The term 'start-thread' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

The same thing happened when I tried start-job.

I also tried fiddling around with 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() 

So, I think the best would be to know what I do wrong when I use start-thread, because it seems to work for other people. I use v2.0 and I don't need downward compatibility.

like image 706
Caesar Avatar asked Jul 24 '10 16:07

Caesar


People also ask

Is multithreading possible in PowerShell?

Starting in PowerShell 7.0, the ability to work in multiple threads simultaneously is possible using the Parallel parameter in the Foreach-Object cmdlet.

Is PowerShell single threaded?

The default PowerShell session is single-threaded. It runs one command and when it finishes, it moves to the next command. This is nice as it keeps everything repeatable and does not use many resources.

How does PowerShell scripting work?

The PowerShell scripting language operates by executing a series of PowerShell commands (or a single one), with each command appearing on a separate line. For the text file to be treated as a PowerShell script, its filename needs to end in . PS1 to connote a PowerShell extension.

What does $_ in PowerShell mean?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


2 Answers

Powershell does not have a built-in command named Start-Thread.

V2.0 does, however, have PowerShell jobs, which can run in the background, and can be considered the equivalent of a thread. You have the following commands at your disposal for working with jobs:

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. 

Here is an example on how to work with it. To start a job, use start-job and pass a script block which contains the code you want run asynchronously:

$job  = start-job { get-childitem . -recurse } 

This command will start a job, that gets all children under the current directory recursively, and you will be returned to the command line immediately.

You can examine the $job variable to see if the job has finished, etc. If you want to wait for a job to finish, use:

wait-job $job 

Finally, to receive the results from a job, use:

receive-job $job 
like image 124
driis Avatar answered Oct 07 '22 15:10

driis


You can't use threads directly like this, but you can't be blamed for trying since once the whole BCL is lying in front of you it's not entirely silly to expect most of it to work :)

PowerShell runs scriptblocks in pipelines which in turn require runspaces to execute them. I blogged about how to roll your own MT scripts some time ago for v2 ctp3, but the technique (and API) is still the same. The main tools are the [runspacefactory] and [powershell] types. Take a look here:

http://www.nivot.org/2009/01/22/CTP3TheRunspaceFactoryAndPowerShellAccelerators.aspx

The above is the most lightweight way to approach MT scripting. There is background job support in v2 by way of start-job, get-job but I figured you already spotted that and saw that they are fairly heavyweight.

like image 35
x0n Avatar answered Oct 07 '22 15:10

x0n