need to call a function at periodic time intervals in c++

前端 未结 8 1110
悲&欢浪女
悲&欢浪女 2020-11-30 08:58

I am writing a program in c++ where I need to call a function at periodic time intervals, say every 10ms or so. I\'ve never done anything related to time or clocks in c++, i

8条回答
  •  一整个雨季
    2020-11-30 09:34

    If you're coding with Visual C++, you could add a timer element to the form you want to call a periodic function (here it's called my form is MainForm, and my timer MainTimer). Add a call to the tick event in the "Events". The designer will add such line in your .h file:

    this->MainTimer->Enabled = true;
    this->MainTimer->Interval = 10;
    this->MainTimer->Tick += gcnew System::EventHandler(this, &MainForm::MainTimer_Tick);
    

    Then, at each interval (specified in ms), the application will call this function

    private: System::Void MainTimer_Tick(System::Object^  sender, System::EventArgs^  e) {
       /// Enter your periodic code there
    }
    

提交回复
热议问题