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