【问题标题】:How to call case in switch every x interval time using gettickcount() c++如何使用gettickcount()c ++每隔x间隔时间在switch中调用case
【发布时间】:2016-08-31 04:30:41
【问题描述】:

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

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;
            }

【问题讨论】:

  • 这里没有足够的信息给你一个好的答案。这是该线程唯一需要做的事情吗?在此过程中,您是否需要进行任何其他处理?
  • 是的,只需要每x次函数GCChatTargetSend(lpObj,lpObj->Index,gServerInfo.m_Message);使用 gettickcount() 执行;
  • gettickcount 是这个工作的错误工具。如果它是由某些外部驱动程序强制要求的,那么,你很糟糕,但如果你可以控制要使用的 API 调用,请考虑使用waitable timersAnother useful link

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


【解决方案1】:

以下是一些代码,用于说明如何将 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-12
    • 2017-03-13
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多