2016-08-31 7 views
0

私はこのスイッチを持っているので、x秒ごとにgettickcount()を使用してC++でcaseを実行する必要があります。事前のおかげで、私のスパングリッシュのため申し訳ありません:Dgettickcount()を使用してxインターバルごとにswitchを呼び出す方法C++

int x = rand() % 4; 
      switch(x) 
      { 
       case 0: 
        GCChatTargetSend(lpObj,lpObj->Index,"String message 1 Here"); 
        break; 

       case 1: 
        GCChatTargetSend(lpObj,lpObj->Index,"String message 2 Here"); 
        break; 

       case 2: 
        GCChatTargetSend(lpObj,lpObj->Index,"String message 3 Here"); 
        break; 

       case 3: 
        GCChatTargetSend(lpObj,lpObj->Index,"String message 4 Here"); 
        break; 
      } 
+0

https://stackoverflow.com/questions/37240834/how-can-we-make-a-loop-with-chronicleの可能性の重複-statement-in-c – Galik

+0

ここにはあなたに良い答えを与えるのに十分な情報がありません。このスレッドが行う必要があるのはこれだけですか?これが起こっている間に他の処理が必要なのですか? – Tas

+0

はい、すべてのx時間関数が必要です。GCChatTargetSend(lpObj、lpObj-> Index、gServerInfo.m_Message); gettickcount()を使用して実行します。 –

答えて

0

enter image description here

は、ここでは、インターバルタイマとして最大GetTickCount()を設定する方法についていくつかのコードです:

まずあなたが時間を取得し、

StartTime = GetTickCount()

他のタイマーを追加する

EndTime = GetTickCount() 

DeltaTime = EndTime - StartTime

のStartTime

から終了時間を引く。そして、あなたが EveryMillisecondTaskDeltaTimeを比較します。結果が 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(); 
} 
関連する問題