Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
No warnings No errors but no workie either.

I am trying to start a thread to implement a non blocking stopwatch style timer.

_beginthread never calls the function TimerGo and the Handle returned is curiously
0xcccccccc or uninitialized.

Header declaration in private section of CBCRMaster

static void TimerGo(void * p_userdata); 


Thread starter in BCRMaster.cpp

HANDLE hThread = _beginthread((void)TimerGo,0,this);


TimerGo function

void CBCRMaster::TimerGo(void * ParentPtr)
{
 Timer timer;
 while(true) {
     if(timer.elasped(3000)) {
         Beep(1000,1000);
         AfxMessageBox("Times Up!");
     }
  }
}


The timer class FWIW

class Timer {
     clock_t counter;
 public:
     Timer(): counter(0) {};
  
     bool elasped(clock_t ms)
     {
         clock_t tick = clock();
  
         if(tick - counter >= ms)
         {
              counter = tick;
              return true;
         }
  
         return false;
     }
 };
Posted
Updated 26-Jul-11 17:28pm
v2
Comments
LaxmikantYadav 27-Jul-11 0:04am    
On Which IDE you are running the program ?
Ron Anders 27-Jul-11 0:12am    
VS '08

_beginthread did work in another area of the same program where a window was attached to this COM DLL when launching it's WindowProc. I have scrapped that notion for a different approach, but copied the way it was done frankly expecting it to work the first time. But it's rarely how it goes.

1 solution

Remove cast to void which is syntactically incorrect, e.g you need:

_beginthread(TimerGo,0,this);

Your entire call is optimized out - check compiler warnings.
 
Share this answer
 
Comments
Ron Anders 27-Jul-11 0:24am    
Ding Ding Ding! or perhaps I should say Beep Beep!

That fixed it.

Fussy little function...

Thank you ever so much.
michaelmel 27-Jul-11 0:31am    
No worries.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900