在C ++中触发事件并在C#中处理事件

Igb*_*Igb 5 .net c# c++ clr c++-cli

我有一台带有一些数字I / O引脚的工业计算机。制造商提供了一些C ++库和示例来处理引脚状态更改。

我需要将此事件集成到C#应用程序中。AFAIK执行此操作最简单的方法是:

  1. 为制造商库制作一个托管的C ++ / CLI包装器,该包装器会在DIO引脚发出中断时触发事件。
  2. 引用该包装器并处理C#部分中的事件,因为它们是正常的C#事件。

我试图用一些没有运气的模拟对象来完成这项工作。从文档来看,在我的案例中,EventHandler函数应该执行大部分“肮脏的工作”。遵循旧线程中可用的信息以及MSDN文档中的EventHandler示例,我最终得到了以下测试代码:

C ++ / CLI

using namespace System;

public ref class ThresholdReachedEventArgs : public EventArgs
{
public:
    property int Threshold;
    property DateTime TimeReached;
};

public ref class CppCounter
{
private:
    int threshold;
    int total;

public:
    CppCounter() {};

    CppCounter(int passedThreshold)
    {
        threshold = passedThreshold;
    }

    void Add(int x)
    {
        total += x;
        if (total >= threshold) {
            ThresholdReachedEventArgs^ args = gcnew ThresholdReachedEventArgs();
            args->Threshold = threshold;
            args->TimeReached = DateTime::Now;
            OnThresholdReached(args);
        }
    }

    event EventHandler<ThresholdReachedEventArgs^>^ ThresholdReached;

protected:
    virtual void OnThresholdReached(ThresholdReachedEventArgs^ e)
    {
        ThresholdReached(this, e);
    }
};

public ref class SampleHandler
{
public:
    static void c_ThresholdReached(Object^ sender, ThresholdReachedEventArgs^ e)
    {
        Console::WriteLine("The threshold of {0} was reached at {1}.",
            e->Threshold, e->TimeReached);
        Environment::Exit(0);
    }
};

void main()
{
    return;
    CppCounter^ c = gcnew CppCounter(20);
    c->ThresholdReached += gcnew EventHandler<ThresholdReachedEventArgs^>(SampleHandler::c_ThresholdReached);

    Console::WriteLine("press 'a' key to increase total");
    while (Console::ReadKey(true).KeyChar == 'a') {
        Console::WriteLine("adding one");
        c->Add(1);
    }
}
Run Code Online (Sandbox Code Playgroud)

C#

using System;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        CppCounter cc = new CppCounter(5);
        //cc.ThresholdReached += cs_ThresholdReached; //<--This is the offending line


        Console.WriteLine("press 'a' key to increase total");
        while (Console.ReadKey(true).KeyChar == 'a')
        {
            Console.WriteLine("adding one");
            cc.Add(1);
        }
    }

    static void cs_ThresholdReached(object sender, ThresholdReachedEventArgs e)
    {
        Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
        Environment.Exit(0);
    }
}

class Counter
{
    private int threshold;
    private int total;

    public Counter(int passedThreshold)
    {
        threshold = passedThreshold;
    }

    public void Add(int x)
    {
        total += x;
        if (total >= threshold)
        {
            ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
            args.Threshold = threshold;
            args.TimeReached = DateTime.Now;
            OnThresholdReached(args);
        }
    }

    protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
    {
        EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
}

  public class ThresholdReachedEventArgs : EventArgs
    {
        public int Threshold { get; set; }
        public DateTime TimeReached { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?是我想念的东西吗?

Han*_*ant 5

  public class ThresholdReachedEventArgs : EventArgs
Run Code Online (Sandbox Code Playgroud)

该代码是正确的,除了这个小故障。您不小心在C#代码中重新声明了该类。现在有两个,一个来自您的C ++ / CLI项目,另一个来自您的C#项目。这是一个问题,.NET中的类型标识不仅由名称空间名称和类名称确定,还包括其来源的程序集。

因此,这是两种不同的类型,编译器会尝试告诉您它的C#版本不是正确的类型。它们具有相同的名称并不能完全帮助您解码错误消息:)

易于修复,只需从C#代码中删除类声明即可。现在,编译器将使用它的C ++ / CLI版本。