我有一台带有一些数字I / O引脚的工业计算机。制造商提供了一些C ++库和示例来处理引脚状态更改。
我需要将此事件集成到C#应用程序中。AFAIK执行此操作最简单的方法是:
我试图用一些没有运气的模拟对象来完成这项工作。从文档来看,在我的案例中,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);
}
} …Run Code Online (Sandbox Code Playgroud)