Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run function in different runspace, PowerShell

I have powershell script that add registration to event, but when the event is jump i'm getting the following error every time the event is jump: "no Runspace available to run scripts in this thread" I'm using PS v2, what can i do? Can you provide some code? Thanks

like image 609
Tom Avatar asked Oct 30 '25 18:10

Tom


1 Answers

It sounds like you're trying to run script on a non-powershell thread, probably via an async callback. This is not a supported scenario. The trick is to "bridge" to callback to the eventing infrastructure, where scriptblocks haev a default runspace. Take a look at what I wrote here:

https://web.archive.org/web/20190222052659/http://www.nivot.org/blog/post/2009/10/09/PowerShell20AsynchronousCallbacksFromNET

It shows you how to work with async threads in powershell and run script in response to callbacks.

-Oisin

like image 75
x0n Avatar answered Nov 03 '25 02:11

x0n



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!