ThreadOptions ReuseThread not working

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mmmoi5
Posts: 14
Last visit: Wed Dec 19, 2018 3:29 am

ThreadOptions ReuseThread not working

Post by mmmoi5 »

Hi,
When I use Sapien products to generate an EXE file of the PowerShell script, the $Host.Runspace.ThreadOptions = "ReuseThread" stops working and it is reporting "The property 'ThreadOptions' cannot be found on this object. Verify that the property exists and can be set"

Is it because it is an EXE, or is there another reason ?

Thank you
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: ThreadOptions ReuseThread not working

Post by davidc »

The host does not support / implement the Runspace property. Why do you need to change the runspace's ThreadOption?
David
SAPIEN Technologies, Inc.
User avatar
mmmoi5
Posts: 14
Last visit: Wed Dec 19, 2018 3:29 am

Re: ThreadOptions ReuseThread not working

Post by mmmoi5 »

When the statement is made from a PS console it works fine, it is only when passed via the EXE file that it reports what I previously stated.
Looking for an explanation to explain that behavior.

You stated that the host does not support or implement runspace property. But it does in a PS console, why not through the same PS code enveloped in an EXE ?


Thank you.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: ThreadOptions ReuseThread not working

Post by davidc »

The console itself is a PowerShell host as well, but when you run an executable, it is using a different host. Unfortunately, Microsoft's documentation is spare on the subject.
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: ThreadOptions ReuseThread not working

Post by davidc »

Apparently this property is not exposed to third party hosts. It is internal to PowerShell.
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ThreadOptions ReuseThread not working

Post by jvierra »

"ReuseThread" can only be set before the runspace is created. Once it is created and a thread is allocated the property is read-only.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ThreadOptions ReuseThread not working

Post by jvierra »

Also note the following about ThreadOptions.
PS D:\scripts> $host.Runspace.ThreadOptions = 1
Exception setting "ThreadOptions": "The thread options can only be changed if the apartment state is multithreaded apartment (MTA), the current
options are UseNewThread or UseCurrentThread, and the new value is ReuseThread."
Also

$host.Runspace.ThreadOptions.value__ = [System.Management.Automation.Runspaces.PSThreadOptions]::UseNewThread

is the correct method for setting this.

As David notes - some host implantations may not have this. You likely need a WMF 3 or later Net Framework host and the apartment type selected (MTA/STA).
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ThreadOptions ReuseThread not working

Post by jvierra »

With the Sapien implementation this is the "$host" in use:
Name : PrimalScriptHostImplementation
Version : 3.0.0.0
InstanceId : 12d52fb0-3fc8-431c-8c72-7aba3be25b4f
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-US
CurrentUICulture : en-US
PrivateData :
DebuggerEnabled : True
IsRunspacePushed :
Runspace :
This topic is 5 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked