C++ / CLR make timer instance available to other functions without separate thread

Alexandru Teodor 91 Reputation points
2022-06-15T10:43:28.923+00:00

In a windows C++ CLR project, on pressing a button (start) I initialize a counter (a chrono timer):

private: System::Void start_Click(System::Object^ sender, System::EventArgs^ e) {  
	clock::time_point start_runtime = clock::now(); // I create a time point "start_runtime"  
        // stuff here  
}  

I am trying to get the time point "start_runtime" to be available in other functions:

double get_timer_seconds(clock::time_point timer_name) {  
	return std::chrono::duration_cast<milliseconds>(clock::now() - timer_name).count() / 1000.0;  
}  
void stop_script() {  
	double secs = get_timer_seconds(start_runtime); // <=== Error (active)	E0020	identifier "start_runtime" is undefined   
	MessageBox::Show(System::Convert::ToString("runtime: " + secs), "", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);  
}  

Is there a way to make the time point created in a private function to be available in other functions without using a separate thread?

Thanks

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
322 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,526 questions
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2022-06-15T11:05:55.027+00:00

    Try something like this:

    private:  
      
       clock::time_point& start_runtime = *new clock::time_point( );  
      
       System::Void start_Click( System::Object^ sender, System::EventArgs^ e)   
       {  
          start_runtime = clock::now( );  
          . . .  
       }  
    

    But there is a series of useful .NET classes and functions that can be used in C++/CLR.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful