Multithreading questions

by Nov 8, 2012

Dear all,

firstly great work on this website.  I have a few questions regarding multithreading.

I based my own code on examples found from the script library.  Regarding my understanding of multithreading for one to use threads he first needs to create a runspace pool in which all threads will live in.  Once this runspace pool is created do all new threads enter here automatically?

$throttleLimit = 10
#do we create an initialsessionstate here that contains an environment with all cmdlets etc?
$iss=[system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$runspacepool = [runspacefactory]::CreateRunspacePool(1, $ThrottleLimit, $iss, $Host)
$runspacepool.Open()

$thread = [powershell]::Create().AddScript($ScriptBlock).AddArgument($someargument)
$thread.RunspacePool = $runspacepool
$thread.BeginInvoke()

here we are creating a thread, what happens if we don't add it to the runspacepool? does it run in some default runspacepool?

Also, for the sake of sanity, rather than define seperate functions in the $scriptBlock, is it somehow possible to pass an initializationblock of some sort containing those functions, that i could also make accessible in the rest of the script?

 

Thank you for your replies

George