问题
the [Powershell]::create()
- method creates a new PowerShell-"Instance" either in the "current or a new runspace".
Can someone explain how the terms process, instance, runspace and (maybe thread) relate to each other in this regard. In laymen's terms if possible?
回答1:
You can think of [Powershell]::Create() as a new powershell session on the separate thread. This session will have some default runspace created but you can change it to another one. Unlike Start-Process (separate process) and Start-Job (child process), [Powershell]::Create() is running on the same process with your main script and sharing memory space with it. This means that you can exchange actual .net objects between the main and child sessions. If sessions run on separate processes they can only exchange with textual/serialized data.
回答2:
The terms you have are not interchangeable and do not do the same thing.
Process is program that runs an instruction set.
Thread is a single running of instructions in a program.
Multi-threading is when multiple instructions are run at the same time. Each requires a separate thread.
Runspace is in the same powershell process but calls a new powershell engine in order to run its code without interfering with the current powershell scripts thread.
Instance is a contained running of code. Its a descriptor.
So Here are some examples
I can have a Instance of a Process. I can have a Instance of a Thread. I Can have a Instance of a Runspance.
Editing to expand on Answer based on comment
"So in the example ive posted above ([Powershell]::create()), is that an instance of a thread, process or runspace?"
So we have a Powershell Application. What is happening is this application starts a Runspace where your commands will be executed and sets up a location to create Powershell Objects. Every time you open the powershell console you are starting another runspace.
The [Powershell]::create()
it creates an object where you can determine what will be run and what runspace it will be run on. If you dont choose a runspace then it will create one for you.
So [Powershell] is the What will run? (The Script) and the Where it will run (A Runspace)
The Runspace is the How will it run? (On a powershell Engine)
来源:https://stackoverflow.com/questions/54503232/process-vs-instance-vs-runspace-in-powershell