如何在C 中执行主程序时是否通过了一分钟是否通过

How to check in background whether a minute has passed while execution of the main program in c++?

本文关键字:是否 通过了 一分钟 主程序 执行      更新时间:2023-10-16

我创建了一个程序,该程序在屏幕上显示当前时间。我现在想做的就是每分钟更新那个时间。

dateAndTime now; //created an object of a custom class dateAndTime
cout << "ntttttt" << now.hour() << ':' << now.minute() << endl;
cout << "CASH MANAGER" << "n";
cout << "1. Add cash" << "n";
cout << "2. Withdraw cash" << "nn";
int ch;
cout << "Enter a number 1 or 2: ";
cin >> ch;

让我们假设时间通过以下过程更新:

if (time(0) == now.epochTime() + 1)
    now.setTime(time(0));

现在,我要做的是每分钟显示为now.hour() << ':' << now.minute();的时间应更新,而不会延迟或中断主程序。

这意味着例如,如果用户在提示后几分钟不提供任何输入,则应在每分钟后自动更新时间,并且在整个过程中,程序始终在等待用户的输入(即主程序主机像往常一样运行)。有什么办法吗?我知道这样做可能没有跨平台技术。所以我只想为 windows os做到这一点。

编辑:更新后不需要恢复当前光标位置或用户的任何书面文本(尚未输入)。

您可以使用alarm()函数,该功能将在时间到期时发送信号SIGALRM

应用程序只需要为该信号带一个信号处理程序。