header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Running threads
Last Post 02 Dec 2008 02:14 PM by x0n. 1 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
stejUser is Offline
New Member
New Member
Posts:1
Avatar

--
14 Aug 2008 10:42 AM  

Hi, I wonder whether PowerShell supports running threads. I haven't found any posts about it, so am I the only one interested? :) If I try to run this script, PowerShell console is killed with message 'An error has occured that was not properly handled. Additional information is shown below. The windows powerShell process will exit.' Nothing more.


Add-Type @'

using System;
using System.Threading;

public class ThreadRunner
{
    public delegate void FiredDelegate(object source, EventArgs args);

    public event FiredDelegate Fired;
    public void Start()
    {
        Thread t = new Thread(Runner);
        t.Start();
    }

    public void Runner()
    {
        for (int i = 0; i < 100; i++)
        {
            Thread.Sleep(1000);
            if (Fired != null)
                Fired(this, EventArgs.Empty);
        }
    }
}


'@

$c = new-object ThreadRunner
$c.add_Fired({  
    write-host 'fired'
  })  
# $c.Start() #causes crash
# $c.Runner() this works, because no thread is executed

Any idea how to get it working? (It's the simplest example just to point to the problem..)

x0nUser is Offline
New Member
New Member
Posts:6
Avatar

--
02 Dec 2008 02:14 PM  
No, this won't work because powershell keeps runspace instances (which it uses to run powershell script) in thread-local storage (TLS). Your thread does not have any runspaces in the TLS required.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer