C# C语言中正确的事件处理#

C# C语言中正确的事件处理#,c#,winapi,event-handling,windows-ce,C#,Winapi,Event Handling,Windows Ce,这基本上是前一个问题()的后续内容。我根据收到的答案和评论修改了代码,解决了最初的问题,即从gpio api将事件传递给GpioSetupInterruptPin。我没有很多关于api的文档,但我试图实现的是:有一个带有白色标签的表单;按下开关后,标签变为黄色。 我现在遇到的问题是,事件似乎在创建后立即触发(“执行”消息被传递到调试对话框,标签变为黄色),但当我切换开关时,它不会做任何事情。在最后一个问题中,我被告知使用WaitForSingleObject,但我真的不确定该在哪里调用它,这只会

这基本上是前一个问题()的后续内容。我根据收到的答案和评论修改了代码,解决了最初的问题,即从gpio api将事件传递给GpioSetupInterruptPin。我没有很多关于api的文档,但我试图实现的是:有一个带有白色标签的表单;按下开关后,标签变为黄色。 我现在遇到的问题是,事件似乎在创建后立即触发(“执行”消息被传递到调试对话框,标签变为黄色),但当我切换开关时,它不会做任何事情。在最后一个问题中,我被告知使用WaitForSingleObject,但我真的不确定该在哪里调用它,这只会增加我的困惑

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);
        Thread waitThread=new Thread(WaitForTrigger);
        waitThread.Start();
        if (!ok)
            Debug.WriteLine("NO interrupt");
        else
            Debug.WriteLine("Interrupt set for:" + port6 + "04" + " at " + hGPIO);
    }

    public static string Trigger = "InputProcessUpdateHandler";
    public static IntPtr handle = CreateEvent(IntPtr.Zero, false, false, Trigger); //used P/Invoke 
    private static InputProcessor inputProcessor = null;

    public Color[] color =
    {
        Color.Orchid, Color.DarkOrchid, Color.GreenYellow, Color.CornflowerBlue, Color.SteelBlue,Color.Crimson
    };

    public int i = 0;

    public void WaitForTrigger()
    {
        while(true)
        {try
        {
            if (WaitForSingleObject(handle, 0xFFFFFFFF) == false)
            {
                BeginInvoke(((System.Action)(() =>label2.BackColor = color[i])));
                i++;
                if (i > 4)
                    i = 0;
            }
            Thread.Sleep(300);
        }
        catch (Exception e)
        { Debug.WriteLine("exception: " + e); }}
        }
    }

    private void _Main()
    {
        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);
      }
   }
如果有人能帮我,我将非常感激


***我让它工作了,但它看起来一团糟。有人能帮我把它弄清楚吗?

你的代码让我很困惑。我想你想要的是这样的东西。请记住,我是在SO文本编辑器中输入的,所以不要期望它能够编译并正常工作——它只是一个指南。把它看作是伪代码之上的一个步骤。< /P>
public class DeviceInterrupt
{
    IntPtr m_gpio;
    string m_eventName;

    public event EventHandler OnInterrupt;

    public DeviceInterrupt(int port)
    {
        // get a driver handle
        m_gpio = GPIO_Open();

        // generate some unique event name
        m_eventName = "GPIO_evt_" + port;

        // wire up the interrupt
        GpioSetupInterruptPin(m_gpio, port, m_eventName, ...);

        // start a listener
        new Thread(EventListenerProc)
        {
            IsBackground = true,
            Name = "gpio listener"
        }
        .Start();
    }

    public void Dispose()
    {
        // TODO: release the handle
    }

    private void EventListenerProc()
    {
        // create the event with the name we sent to the driver
        var wh = new WaitHandle(false, m_eventName);

        while (true)
        {
            // wait for it to get set by the driver
            if (wh.WaitOne(1000))
            {
                // we have an interrupt
                OnInterrupt.Fire(this, EventArgs.Empty);
            }
        }
    }
}
使用方法如下所示:

var intr = new DeviceInterrupt(4);
intr.OnInterrupt += MyHandler;
....
void MyHandler(object sender, EventArgs a)
{
    Debug.WriteLine("Interrupt occurred!");
}
注意


紧凑型框架不支持实际的命名系统事件,因此我在上面的代码中使用的命名的
WaitHandle
不是CF提供的
WaitHandle
。相反,我使用的是来自。您也可以自己P/Invoke到
CreateEvent
WaitForSingleObject

我不清楚为什么您有额外的两个类,这两个类似乎只是封装事件和处理程序。事件可以说应该位于封装GPIO功能的同一个类中(不清楚您是否实际拥有其中一个),并且处理程序本身可以位于
Form1
类中。这就是说,这里最大的问题似乎是您缺少任何机制来接收来自GPIO实现的信号。最接近您的方法似乎是创建
句柄
实例,但它似乎根本没有被使用,更不用说传递给GPIO了。很抱歉,我的回复延迟了很长时间(家庭紧急情况和大量工作)。代码相当混乱,因为我开始为代表和事件做一些示例,上面的代码只是我尝试将它们结合起来。嗨,谢谢你的指导,因为它使事情变得有点清楚。请你帮我处理一下WaitHandle()好吗?我尝试过使用它,但我得到一个错误“无法在这里访问受保护的构造函数”。我尝试过使用EventWaitHandle(),但无法正常工作。您不能使用CF提供的WaitHandle。CF团队的无限愚蠢导致他们不支持实际的命名事件,因此您必须自己使用P/Invoke来调用API,或者使用(免费、开源)SDF版本-位于