在静脉 omnet 中使用 timerManager 时如何取消计时器

how to cancel a timer when using timerManager in veins omnet

我正在使用静脉中提供的 TimerManager class 来管理我的模拟中的计时器。 我创建了一个每 5 秒触发一次的循环计时器,我想在收到特定消息时取消它,但我不知道如何使用取消方法:

void cancel(TimerHandle handle)

我不明白什么是 TimerHandle 以及我应该向该方法传递什么参数才能取消计时器

PS:我想取消一个特定的定时器而不是 TimerManager 实例

创建定时器时,TimerManager::createreturns一个TimerHandle。您可以通过将其传递给 TimerManager::cancel.

来使用它来取消计时器

@Horstinator 这是我的代码:RSU 的申请

    #ifndef RSUAPP_H_
    #define RSUAPP_H_
    #include <omnetpp.h>
    #include "veins/modules/application/ieee80211p/BaseWaveApplLayer.h"
    #include "ttr_m.h"
    #include "veins/modules/utility/TimerManager.h"

    using namespace omnetpp;
    using Veins::TimerManager;

    class rsuApp : public BaseWaveApplLayer {
    public:
    rsuApp();
    ~rsuApp();
    virtual void initialize(int stage);
    virtual void finish();
    protected:
    virtual void onBSM(BasicSafetyMessage* bsm);
    virtual void onWSM(WaveShortMessage* wsm);
    virtual void onWSA(WaveServiceAdvertisment* wsa);
    virtual void handleSelfMsg(cMessage* msg);
    virtual void handlePositionUpdate(cObject* obj);
//declaration of the TimerManager instance and the handle
    TimerManager timerManager{this};
    TimerManager::TimerHandle handle;
    };
   #endif

.

   #include "rsuApp.h"
   #include "ttr_m.h"
   #include "veins/modules/utility/TimerManager.h"
   #include <map>

   Define_Module(rsuApp);

   rsuApp::rsuApp(){}
   void rsuApp::initialize(int stage) {
   BaseWaveApplLayer::initialize(stage);
     auto ttrTimer = [this](){
                Ttr* ttr = new Ttr();
                populateWSM(ttr);
                sendDown(ttr);
          };
       auto ttrTimerSpec = Veins::TimerSpecification(ttrTimer).interval(1);
       handle=timerManager.create(ttrTimerSpec, "recurring timer");
   }

   void rsuApp::finish() {
     BaseWaveApplLayer::finish();
   }

   void rsuApp::onWSM(WaveShortMessage* wsm) {
     if(strcmp(wsm->getName(),"tip")==0){
       findHost()->getDisplayString().updateWith("r=60,red");
   //canceling the timer when a wsm message  is received 
       timerManager.cancel(handle);
     }
   }

   void rsuApp::handleSelfMsg(cMessage* msg) {
    BaseWaveApplLayer::handleSelfMsg(msg);
     timerManager.handleMessage(msg);  
   }

  void rsuApp::onBSM(BasicSafetyMessage* bsm) {}
  void rsuApp::onWSA(WaveServiceAdvertisment* wsa) {}
  void rsuApp::handlePositionUpdate(cObject* obj) {
   BaseWaveApplLayer::handlePositionUpdate(obj);
  }
  rsuApp::~rsuApp(){}