Fez Cobra II - LDR1 button

I’m trying to used the LDR1 button on my Fez Cobra to stop all the threads running on the board. I’ve setup the code below to create an event for the button press:


     public void ProgramStarted()
    {
    // Mainboard buttons
      InterruptPort ldr1 = new InterruptPort(Pin.P0_22, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
      ldr1.OnInterrupt += ldr1_Pressed;
    }

    private void ldr1_Pressed(uint data1, uint data2, DateTime time)
    {
      // LDR1 = Shutdown
      Shutdown();
    }

While my app is idle it recognised a button press and fires the event but as soon as the board becomes busy the event never fires.

Can anyone explain what is going on, and is there a way to respond to a button press even when the board is busy?

By the way, when I say ‘busy’ I don’t mean that the cpu is at 100% but the board is doing it’s job watching for input and sending data via the network.

Try to declare InterruptPort at the class level so it will not be GCed after ldr1 goes out of scope at the end of the ProgramStarted.

@ Slade -

how about if add:

If the event fires, and the port is garbage-collected, will that not fire an exception?

Ah! I assumed that if there was an event handler assigned it wouldn’t get GC’d.

Thanks guys, I’ll make the required changes.