ThreadOptions ReuseThread not working

Ask your Windows 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.
Locked
User avatar
mmmoi5
Posts: 14
Joined: Sat May 27, 2017 8:56 am

ThreadOptions ReuseThread not working

Post by mmmoi5 » Wed Dec 12, 2018 12:43 pm

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
Joined: Thu Aug 18, 2011 4:56 am

Re: ThreadOptions ReuseThread not working

Post by davidc » Fri Dec 14, 2018 8:23 am

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
Joined: Sat May 27, 2017 8:56 am

Re: ThreadOptions ReuseThread not working

Post by mmmoi5 » Wed Dec 19, 2018 4:29 am

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
Joined: Thu Aug 18, 2011 4:56 am

Re: ThreadOptions ReuseThread not working

Post by davidc » Wed Jan 02, 2019 8:23 am

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
Joined: Thu Aug 18, 2011 4:56 am

Re: ThreadOptions ReuseThread not working

Post by davidc » Wed Jan 02, 2019 8:30 am

Apparently this property is not exposed to third party hosts. It is internal to PowerShell.
David
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: ThreadOptions ReuseThread not working

Post by jvierra » Wed Jan 02, 2019 9:01 am

"ReuseThread" can only be set before the runspace is created. Once it is created and a thread is allocated the property is read-only.

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: ThreadOptions ReuseThread not working

Post by jvierra » Wed Jan 02, 2019 9:10 am

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).

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: ThreadOptions ReuseThread not working

Post by jvierra » Wed Jan 02, 2019 9:44 am

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 :

Locked