Stefan Cazacu

Stefan Cazacu

  • NA
  • 28
  • 27.1k

Correct event handling and waiting in C#

Nov 28 2014 7:20 AM
Hello. This is a follow up on a previously posted question (http://www.c-sharpcorner.com/Forums/Thread/276805/). I've revised my code and despite it not throwing an exception any more, it doesn't seem to pick up the event. I don't have a lot of documentation on the api but what i'm trying to achieve is: have a form with a white label; after pressing a switch, the label should turn yellow. I've created an event and passed it to the GpioSetupInterruptPin function, configured to trigger on a rising edge. 
As soon as i call GpioSetupInterruptPin, the event triggers and doesn't seem to reset. To me, this suggests there is a problem with the event, the waiting for the event or the api doesn't really work. I've managed to contact the developer of the api but he said others have successfully used the api, that he doesn't have experience using C# and offered no additional information on how to use the api or how it works.
Can someone please offer a suggestion on how i could fix my code?
 
public partial class Form1 : Form
{
// P/Invoke CreateEvent and WaitForSingleObject
private void GPIO_Open() //get handle for gpio
private void GPIO_Output() //output pin declaration
private void button1_Click(object sender, EventArgs e)
{
Interrupt_Setup();
}
private void Interrupt_Setup()
{
hGPIO = GPIOapi.GpioOpenHandle(); //returns a handle to the gpio
GIPO_ON = true;
Debug.WriteLine("Driver open \n" + hGPIO);
GPIO_Output(); //set output pins
GPIO_Interrupt(Trigger); //configure interrupt
}
private void GPIO_Interrupt(string trigger)
{
bool ok;
_Main();
//INTERRUPT DECALRATION
ok = GPIOapi.GpioSetupInterruptPin(hGPIO, port6, 4, GPIOapi.INT_TRIGGER_MODE.TRIGGER_MODE_EDGE,
GPIOapi.INT_TRIGGER_POLARITY.TRIGGER_POL_HIGH_RISING, trigger, true);
if (!ok)
Debug.WriteLine("NO interrupt");
else
Debug.WriteLine("Interrupt set for:" + port6 + "04" + " at " + hGPIO);
}
public static string Trigger = "InputProcessUpdateHandler";
private static InputProcessor inputProcessor = null;
private void _Main()
{
public static IntPtr handle = CreateEvent(IntPtr.Zero, false, false, Trigger); //used P/Invoke
try
{
if (WaitForSingleObject(handle, 0xFFFFFFFF) == false)
label2.BackColor = Color.Red;
}
catch(Exception e)
{Debug.WriteLine("exception: "+e);} 
inputProcessor = new InputProcessor();
ShowToggle showToggle = new ShowToggle(inputProcessor);
inputProcessor.Process(label1);
}
public class ShowToggle
{
private InputProcessor _inputProcessor = null;
public ShowToggle(InputProcessor inputProcessor)
{
_inputProcessor = inputProcessor;
_inputProcessor.updateHandledBy += InputProcessUpdateHandler;
}
private void InputProcessUpdateHandler(Label label)
{
label.BackColor = Color.Yellow;
Debug.Write("execute");
}
}
public class InputProcessor
{
public delegate void InputProcessUpdateHandler(Label label);
public event InputProcessUpdateHandler updateHandledBy = null;
public void Process(Label label)
{
if (updateHandledBy != null)
updateHandledBy(label);
}
}

Answers (9)