0


5分ごとに関数を呼び出したかった

AutoFunction(){
    cout << "Auto Notice" << endl;
    Sleep(60000*5);
}

while(1){

    if(current->tm_hour == StartHour && current->tm_min == StartMinut && current->tm_sec == StartSec){
        CallStart();
    }

    AutoFunction();
    Sleep(1000);
}

while1秒ごとに同時に更新したいcall AutoFunction(); 5分ごと、ただしSleepAutoFunctionで待機せずに

別の関数を開始する時間を確認するために、while(1)を1秒ごとに更新する必要があるためです。

私はそれを次のようにしようと思った

while(1){

    if(current->tm_hour == StartHour && current->tm_min == StartMinut && current->tm_sec == StartSec){
        CallStart();
    }

    Sleep(1000);
}
while(1){

    AutoFunction();
    Sleep(60000*5);
}

でも私はそうは思わないので両方が一緒に働くでしょう

ありがとうございました

4

1 に答える 1

1

スレッドとBoostライブラリに慣れていない私たちの場合、これは単一のwhileループで実行できます。

void AutoFunction(){
    cout << "Auto Notice" << endl;
}

//desired number of seconds between calls to AutoFunction
int time_between_AutoFunction_calls = 5*60;

int time_of_last_AutoFunction_call = curTime() - time_between_AutoFunction_calls;

while(1){
    if (should_call_CallStart){
        CallStart();
    }

    //has enough time elapsed that we should call AutoFunction?
    if (curTime() - time_of_last_AutoFunction_call >= time_between_AutoFunction_calls){
        time_of_last_AutoFunction_call = curTime();
        AutoFunction();
    }
    Sleep(1000);
}

このコードでcurTimeは、Unixタイムスタンプをintとして返す関数です。選択したタイムライブラリから適切なものに置き換えます。

于 2012-11-26T21:04:12.213 に答える