如何使用gettickcount()c ++在每隔x个时间间隔调用switch中的case

时间:2016-08-31 04:30:41

标签: c++ time switch-statement intervals gettickcount

我有这个开关,我需要在c ++中使用gettickcount()每x秒执行一次。在此先感谢,对不起我的英语:D

public class PorterList
{
    private ArrayList<Porter> porList; 

    public PorterList()
    {

     porList = new ArrayList<>(); 

    }
    public PorterList(ArrayList<Porter> p)
    {
         porList = p;
    }

}

1 个答案:

答案 0 :(得分:0)

enter image description here

以下是一些代码,用于将GetTickCount( )设置为间隔计时器:

首先,你有时间,

StartTime = GetTickCount( )

然后添加另一个计时器

EndTime = GetTickCount( )

并从StartTime中减去EndTime

DeltaTime = EndTime - StartTime

然后您将DeltaTimeEveryMillisecondTask进行比较。如果结果为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";
}

以下是如何使用Windows自己的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();
}