【发布时间】:2012-12-08 12:12:07
【问题描述】:
我正在使用 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.
}
}
【问题讨论】:
-
我认为加入是不必要的。子线程正在恢复主线程然后退出。无论如何都应该完成子线程。连接时子线程的状态是什么(参见调试线程窗口)?
-
顺便说一句:msdn.microsoft.com/en-us/library/… 见备注部分的注意事项。这并不是执行同步的好方法。此外,您在循环的每次迭代中都订阅了 OnInterrupt 事件。这会导致问题。
-
顺便说一句,为什么所有线程都放在首位?
-
@Pete 感谢您的回复。我现在使用 AutoResetEvent 而不是suspend() 和resume()。另一件事我不应该在循环的每次迭代中订阅 OnInterrupt,但我认为新的 NativeEventHandler(ButtonEvent) 对象应该在每次完成时从内存中释放,所以任何时候都只会有一个实例,不是吗是吗?
-
@Pete 我真的不懂面向对象,我是一名 C 程序员。这就是为什么当谈到 OO 时,我迷路了。那么“首先是线程的东西”是什么意思?我该如何改变呢?
标签: c# multithreading netduino