我正在使用 Netduino 板,但在结束 NativeEventHandler(button) 时遇到问题。问题是主线程卡在 join() 函数上。我不明白为什么子线程在运行后没有被释放。
public class Program
{
private static Thread mainThread;
private static Thread childThread;
private static InterruptPort button = new InterruptPort(Pins.GPIO_PIN_D1, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLevelHigh);
private static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
public static void Main()
{
mainThread = Thread.CurrentThread;
Thread.Sleep(1000);
while (true)
{
Thread.Sleep(100);
button.OnInterrupt += new NativeEventHandler(ButtonEvent);
mainThread.Suspend();
childThread.Join();//It stuck here.
Thread.Sleep(100);
button.EnableInterrupt();
button.ClearInterrupt();
}
}
private static void ButtonEvent(uint port, uint state, DateTime time)
{
childThread = Thread.CurrentThread;
button.DisableInterrupt();
mainThread.Resume();
// Thread.CurrentThread.Abort(); this .Abort() seems doesn't terminate the thread either.
}
}