所以,我正在尝试创建一个计时器事件(这次是在 C++/clr 中),但我不知道如何定义一个新事件,这就是我得到的:
namespace hook
{
public ref class Tick
{
private:
static System::Timers::Timer^ aTimer;
public:
event EventHandler^ OnTick;
int Interval;
Tick()
{
aTimer = gcnew System::Timers::Timer(Interval);
aTimer->Elapsed += gcnew ElapsedEventHandler(Tick::execute);
}
static void execute(Object^ source, ElapsedEventArgs^ e)
{
this->OnTick(this, new EventsArg()); // Wrong
}
};
}
最佳答案
您正在尝试从无法工作的静态方法访问实例成员 (OnTick)。删除 static 关键字以获取:
void execute(Object^ source, ElapsedEventArgs^ e)
这需要您更改事件订阅代码,您必须创建一个存储这个的委托(delegate):
aTimer->Elapsed += gcnew ElapsedEventHandler(this, &Tick::execute);
还有一个错误,您拼错了 EventArgs。 pass 很常见,它有一个预煮对象,由 Empty 返回。减少垃圾:
this->OnTick(this, EventArgs::Empty);
当心编写只是复制原始类而没有增加值(value)的代码。
关于c++ - 在 c++/clr 中创建一个定时器事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12466730/