C++, MFC loop until value is valid or timeout

Noah Aas 625 Reputation points
2025-02-13T19:55:45.0833333+00:00

I am looking for a good way to check a value within a period of time. In principle, the code below works. Are there possibilities with CTime, MFC functions? Why is this written? atlTraceGeneral Is that the TRACE macro?

time\timedlg.cpp(186) : atlTraceGeneral - 0
time\timedlg.cpp(193) : atlTraceGeneral - 16:10:01
time\timedlg.cpp(186) : atlTraceGeneral - 1
time\timedlg.cpp(193) : atlTraceGeneral - 16:10:02
time\timedlg.cpp(186) : atlTraceGeneral - 2
time\timedlg.cpp(193) : atlTraceGeneral - 16:10:03
time\timedlg.cpp(186) : atlTraceGeneral - 3
time\timedlg.cpp(193) : atlTraceGeneral - 16:10:04
time\timedlg.cpp(186) : atlTraceGeneral - 4
time\timedlg.cpp(193) : atlTraceGeneral - 16:10:05
time\timedlg.cpp(186) : atlTraceGeneral - 5
time\timedlg.cpp(193) : atlTraceGeneral - 16:10:06
time\timedlg.cpp(186) : atlTraceGeneral - 6
time\timedlg.cpp(193) : atlTraceGeneral - 16:10:08
time\timedlg.cpp(186) : atlTraceGeneral - 7
time\timedlg.cpp(193) : atlTraceGeneral - 16:10:09

void CTimeDlg::OnBnClickedButtonTime()
{
	auto now = std::chrono::steady_clock::now;
	using namespace std::chrono_literals;
	auto work_duration = 15s;
	auto start = now();

	int i = 0;
	TRACE("START");

	CString sTime;
	CString t = CTime::GetCurrentTime().Format("%H:%M:%S");	
	TRACE1("%s\n", t);

	bool resultMES = false;
	while ((now() - start) < work_duration)
	{
		TRACE1("%d\n", i);
		
		// Check value is true, then break

		Sleep(1000);
		t = CTime::GetCurrentTime().Format("%H:%M:%S");
		TRACE1("%s\n", t);
		i++;
	};
	TRACE("END");
	t = CTime::GetCurrentTime().Format("%H:%M:%S");
	TRACE1("%s\n", t);

}
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,856 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.