は、ここでは、インターバルタイマとして最大GetTickCount()
を設定する方法についていくつかのコードです:
まずあなたが時間を取得し、
StartTime = GetTickCount()
他のタイマーを追加する
EndTime = GetTickCount()
と
DeltaTime = EndTime - StartTime
のStartTime
から終了時間を引く。そして、あなたが
EveryMillisecondTask
で
DeltaTime
を比較します。結果が
true
の場合、設定したswitchステートメントの数字に対応する乱数が生成され、
GCChatTargetSend()
が呼び出されます。ここで
if (DeltaTime >= EveryMillisecondTask){
int x = rand() % 4;
switch(x){
// do stuff
}
}
はGetTickCount()
のために完全なコードリストである:ここでは
#include <time.h>
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
void GCChatTargetSend(string message);
int main() {
int EveryMillisecondTask=1000; // 1 second = 1000 milliseconds
int StartTime = GetTickCount();
srand (time(NULL));
cout<<"Welcome to GetTickCount() interval timer \n \n";
cout<<"Interval set to "<< EveryMillisecondTask <<" milliseconds \n";
while(true){
int EndTime = GetTickCount();
int DeltaTime = EndTime - StartTime;
// test to see if EveryMillisecondTask matches time
if (DeltaTime >= EveryMillisecondTask){
// generate random number
int x = rand() % 4;
cout<<"\nRandom X= "<< x+1 <<"\n";
// switch x
switch(x){
case 0:
GCChatTargetSend("String message 1 Here ");
break;
case 1:
GCChatTargetSend("String message 2 Here ");
break;
case 2:
GCChatTargetSend("String message 3 Here ");
break;
case 3:
GCChatTargetSend("String message 4 Here ");
break;
default:
break;
}
// reset time
StartTime = GetTickCount();
EndTime = GetTickCount();
}
}//while
return 0;
}
void GCChatTargetSend(string message){
cout<<message<<" \n";
}
は窓がsettimer()
機能を所有して使用してインターバルタイマーを行う方法です。
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx
settimer()
のためのサンプルコード:
#define STRICT 1
#include <windows.h>
#include <iostream.h>
VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime);
int main(int argc, char *argv[], char *envp[]){
int Counter=0;
MSG Msg;
int timeInMilliseconds = 2000;
cout<<"Welcome to SetTimer() interval timer \n \n";
cout<<"Interval set to "<< timeInMilliseconds <<" milliseconds \n\n\n";
UINT TimerId = SetTimer(NULL, 0, timeInMilliseconds, &TimerProc); //2000 milliseconds
cout << "TimerId: " << TimerId << '\n';
if (!TimerId) return 16;
while (GetMessage(&Msg, NULL, 0, 0)){
++Counter;
if (Msg.message == WM_TIMER) cout << "Counter: " << Counter << "; timer message\n";
else
cout << "Counter: " << Counter << "; message: " << Msg.message << '\n';
DispatchMessage(&Msg);
}
KillTimer(NULL, TimerId);
return 0;
}
VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime){
cout << "CALLBACK " << dwTime << " \n\n";
cout.flush();
}
https://stackoverflow.com/questions/37240834/how-can-we-make-a-loop-with-chronicleの可能性の重複-statement-in-c – Galik
ここにはあなたに良い答えを与えるのに十分な情報がありません。このスレッドが行う必要があるのはこれだけですか?これが起こっている間に他の処理が必要なのですか? – Tas
はい、すべてのx時間関数が必要です。GCChatTargetSend(lpObj、lpObj-> Index、gServerInfo.m_Message); gettickcount()を使用して実行します。 –