C++/CLI 中的计时器
Timer in C++/CLI
我试图在我的 C++/CLI 应用程序中查找计时器(我使用 windows 形式)。计时器会不断更新标签中的信息。我需要在不冻结表格的情况下使用计时器。我正在尝试通过 std::this_thread 执行此操作。但是当我在 MyForm.cpp 中调用 funtion timerss() 时,我收到错误 E0245“非静态成员引用必须相对于特定对象”。我是这方面的初学者,如果解决困难,你能教我一步步解决问题吗?
timer.hpp
#pragma once
#ifndef TIMERHPP
#define TIMERHPP
#include <chrono>
#include <functional>
class Timerr {
public:
Timerr();
void add(std::chrono::milliseconds delay,
std::function<void()> callback,
bool asynchronous = true);
};
#endif
timer.cpp
// timer.cpp
#include "timer.hpp"
#include <thread>
Timerr::Timerr() {
}
void Timerr::add(std::chrono::milliseconds delay,
std::function<void()> callback,
bool asynchronous) {
if (asynchronous) {
std::thread([=]() {
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
callback();
}).detach();
}
else {
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
callback();
}
}
函数调用(MyForm.cpp):
#include "MyForm.h"
#include <Windows.h>
#include "timer.hpp"
#include <iostream>
using namespace Client;
void foo()
{
Client::MyForm::timerss();//E0245 "Nonstatic member reference must be relative to specific object"
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
Timerr timer;
timer.add(std::chrono::milliseconds(500), foo);
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew MyForm);
return 0;
}
MyForm.h
public: void timerss()
{
timers(1);
}
在命名空间中:
using namespace System;
using namespace System::Threading;
初始化组件:
public: MyForm(void)
{
InitializeComponent();
SetTimeThread = gcnew Thread(gcnew ThreadStart(this, &MyForm::SetTime)); //added
SetTimeThread->IsBackground = true; //added
SetTimeThread->Start(); //added
}
在class MyForm的最后
void SetTime() {
while (true) {
if (this->InvokeRequired) {
this->Invoke(gcnew Action(this, &MyForm::timerss));// Instead timerss - your method
}
Thread::Sleep(30*1000);
}
}
private: System::Threading::Thread^ SetTimeThread;
我试图在我的 C++/CLI 应用程序中查找计时器(我使用 windows 形式)。计时器会不断更新标签中的信息。我需要在不冻结表格的情况下使用计时器。我正在尝试通过 std::this_thread 执行此操作。但是当我在 MyForm.cpp 中调用 funtion timerss() 时,我收到错误 E0245“非静态成员引用必须相对于特定对象”。我是这方面的初学者,如果解决困难,你能教我一步步解决问题吗?
timer.hpp
#pragma once
#ifndef TIMERHPP
#define TIMERHPP
#include <chrono>
#include <functional>
class Timerr {
public:
Timerr();
void add(std::chrono::milliseconds delay,
std::function<void()> callback,
bool asynchronous = true);
};
#endif
timer.cpp
// timer.cpp
#include "timer.hpp"
#include <thread>
Timerr::Timerr() {
}
void Timerr::add(std::chrono::milliseconds delay,
std::function<void()> callback,
bool asynchronous) {
if (asynchronous) {
std::thread([=]() {
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
callback();
}).detach();
}
else {
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
callback();
}
}
函数调用(MyForm.cpp):
#include "MyForm.h"
#include <Windows.h>
#include "timer.hpp"
#include <iostream>
using namespace Client;
void foo()
{
Client::MyForm::timerss();//E0245 "Nonstatic member reference must be relative to specific object"
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
Timerr timer;
timer.add(std::chrono::milliseconds(500), foo);
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew MyForm);
return 0;
}
MyForm.h
public: void timerss()
{
timers(1);
}
在命名空间中:
using namespace System;
using namespace System::Threading;
初始化组件:
public: MyForm(void)
{
InitializeComponent();
SetTimeThread = gcnew Thread(gcnew ThreadStart(this, &MyForm::SetTime)); //added
SetTimeThread->IsBackground = true; //added
SetTimeThread->Start(); //added
}
在class MyForm的最后
void SetTime() {
while (true) {
if (this->InvokeRequired) {
this->Invoke(gcnew Action(this, &MyForm::timerss));// Instead timerss - your method
}
Thread::Sleep(30*1000);
}
}
private: System::Threading::Thread^ SetTimeThread;