multithreading - How does threading in powershell work? -
i want parallelize file-parsing actions network activity in powershell. quick google it, start-thread looked solution, but:
the term 'start-thread' not recognized name of cmdlet, function, script file, or operable program. check spelling of name, or if path included, verify path correct , try again.
the same thing happened when tried start-job.
i tried fiddling around system.threading.thread
[system.reflection.assembly]::loadwithpartialname("system.threading") #this next errors, arguments can't figure out documentation of .net $tstart = new-object system.threading.threadstart({dosomething}) $thread = new-object system.threading.thread($tstart) $thread.start()
so, think best know wrong when use start-thread, because seems work other people. use v2.0 , don't need downward compatibility.
powershell not have built-in command named start-thread.
v2.0 does, however, have powershell jobs, can run in background, , can considered equivalent of thread. have following commands @ disposal working jobs:
name category synopsis ---- -------- -------- start-job cmdlet starts windows powershell background job. get-job cmdlet gets windows powershell background jobs running in current ... receive-job cmdlet gets results of windows powershell background jobs in curren... stop-job cmdlet stops windows powershell background job. wait-job cmdlet suppresses command prompt until 1 or of windows powershell... remove-job cmdlet deletes windows powershell background job.
here example on how work it. start job, use start-job , pass script block contains code want run asynchronously:
$job = start-job { get-childitem . -recurse }
this command start job, gets children under current directory recursively, , returned command line immediately.
you can examine $job
variable see if job has finished, etc. if want wait job finish, use:
wait-job $job
finally, receive results job, use:
receive-job $job
Comments
Post a Comment