0

i have this switch, i need execute case every x seconds using gettickcount() in c++. Thanks in advance, sorry for my spanglish :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;
            }
  • Possible duplicate of https://stackoverflow.com/questions/37240834/how-can-we-make-a-loop-with-chronicle-statement-in-c – Galik Aug 31 '16 at 04:36
  • There isn't enough information here to give you a good answer. Is this the only thing this thread needs to do? Do you require any other processing to happen while this is going on? – Tas Aug 31 '16 at 04:37
  • yes, only need every x time function GCChatTargetSend(lpObj,lpObj->Index,gServerInfo.m_Message); execute using gettickcount(); – Julian Albarracin Cruz Aug 31 '16 at 04:52
  • 1
    `gettickcount` is the wrong tool for this job. If it's mandated by some external driver, well, sucks to be you, but if you have some control over what API calls to use, consider using [waitable timers](https://msdn.microsoft.com/en-us/library/windows/desktop/ms687012(v=vs.85).aspx). [Another useful link](https://msdn.microsoft.com/en-us/library/windows/desktop/ms687008(v=vs.85).aspx) – user4581301 Aug 31 '16 at 05:14

1 Answers1

0

enter image description here

Here is some code for how you set GetTickCount( ) up as an interval timer:

First you get a time,

StartTime = GetTickCount( )

Then you add another timer

EndTime = GetTickCount( )

and subtract EndTime from StartTime

DeltaTime = EndTime - StartTime

And then you compare DeltaTime with EveryMillisecondTask. If the result is true, you generate a random number that corresponds to the numbers in the switch statement you set, which in turn will call GCChatTargetSend( ).

if ( DeltaTime >= EveryMillisecondTask ){
    int x = rand() % 4;

       switch(x){
          // do stuff
       }
}

Here is the complete code listing for 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";
}

Here is how to do an interval timer using windows own settimer( ) function.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx

Sample code for 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();
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28