c# - 在 C++ 中触发事件并在 C# 中处理它们

标签 c# c++ .net c++-cli clr

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

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

  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);
    }
}

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; }
    }
}

我做错了什么?我缺少什么吗?

最佳答案

  public class ThresholdReachedEventArgs : EventArgs

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

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

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

关于c# - 在 C++ 中触发事件并在 C# 中处理它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50674904/

相关文章:

C# - 寻找一种简单的方法来解析两种不同表示形式(数字和英文)之间的字符串值

c++ - malloc如何分配内存以及Heap的大小是多少?

c# - 是否可以控制 .NET Progress<T> 对象使用的 SynchronizationContext?

c# - 在 C# 中将 RGB 颜色转换为最接近的 ACI 颜色

c# - 使用 MVC3 和 EF4 CTP5w 进行 JSON 序列化的循环引用异常

c# - 如何将日期从 yyyyMMdd 格式转换为 mm-dd-yyyy 格式

c# - 比 Marshall.QueryInterface 更简洁的编码方式?

c++ - 比较代码算法/片段的最正确(最好)的方法是什么?

c++ - Push_back 方法在此代码中的工作方式不同。为什么?

c# - 如何连接 xaml 和 xaml.cs 文件