Powershell thread handling problem

Question:

I have a powershell problem, and maybe someone can help me. I’m using powershell 2.0, and i want to create, and use threads. I know that i can use jobs, but that is not what i want.
I want a script, that creates windows forms, and runs background threads too. Since forms needs STA, this is not easy. Running “powershell.exe -sta” is not a solution.

Below is my script that I wrote, for simple thread handling. But it doesn’t work. Even the new thread wont be created. Any suggestion, what is wrong? Please help me if you can!

Regards, Peter.

Answer:

Noticed a couple of mistakes in your script. Firstly $thread_job, try this instead:

You need to put the brackets around ThreadProc or it will be evaluated rather than passed as a function. Secondly you can just specify the type for delegates like ThreadStart and PowerShell will convert things for you; no need for New-Object.

Secondly CurrentThread is a static member – I’m guessing $thread.CurrentThread is a typo and you meant:

I imagine you’ll still have problems getting it to work though – whenever I’ve tried using threads in PowerShell before I’ve always had nasty crashes with no real explanation…

Can you write a cmdlet in C# and call into that instead? Might be easier to do things that way – you could setup a new Runspace and run your command in the other Runspace’s thread.

EDIT

Found these links that might help you:

http://weblogs.asp.net/adweigert/archive/2008/04/29/powershell-threading-for-powershell-v1-0.aspx
http://blogs.msdn.com/b/powershell/archive/2007/03/23/thread-apartmentstate-and-powershell-execution-thread.aspx

Source:

Powershell thread handling problem by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply