If I call Debug.GC(true) the code freeze.
It’s one of those hard freezes that the debuger cant resolve.
The problem seams to happen when there is a WaitHandler some where in the program that is in WaitOne state, If there is a WaitOne in a thread a different thread will get this problem.
I wrote a test code to illustrate where it freezes.
using System;
using Microsoft.SPOT;
using System.Threading;
using System.Collections;
namespace racedisplay.NETMF.Test
{
public class Program
{
public static void Main()
{
Debug.GC(false); // OK
Threadout threadout1 = new Threadout();
Debug.GC(false); // NOT OK, freeze
Threadout threadout2 = new Threadout();
Thread.Sleep(1000);
Timeout timeout = new Timeout();
}
}
public class Signal
{
private ManualResetEvent signal;
private Object locker = new Object();
public Signal()
{
signal = new ManualResetEvent(false);
Thread setter = new Thread(new ThreadStart(Worker));
setter.Start();
}
public void Wait(int id)
{
lock (locker)
{
Debug.Print("Wait for " + id.ToString());
signal.WaitOne();
signal.Reset();
Debug.Print("Done for " + id.ToString());
}
}
private void Worker()
{
while (true)
{
//signal.Set();
Thread.Sleep(4000);
}
}
}
public class Threadout
{
private Thread thread;
private Signal signal;
public Threadout()
{
signal = new Signal();
thread = new Thread(new ThreadStart(TestThread));
thread.Priority = ThreadPriority.Normal;
thread.Start();
}
private void TestThread()
{
while (true)
{
signal.Wait(1);
Thread.Sleep(100);
}
}
}
public class Timeout
{
private Timer timer;
private Signal signal;
public Timeout()
{
signal = new Signal();
timer = new Timer(TestTimer, null, 1000, 2000);
}
private void TestTimer(object state)
{
signal.Wait(2);
}
}
}