Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 596
  • Last Modified:

class derived from CWinThread

I have a class that i derived from CWinThread.  I have a dialog box app that starts two threads (or more) when it starts.  these threads execute some code that then wait a certain amount of time before it repeats the process.  I am using the Sleep function to wait the certain amount of time.

What i would like to do is modifiy the code so that it works like this.  the thread executes the code and then waits a certain amount of time.  While it is waiting it can recieve messages. the message that it should be waiting to recieve would be execute code now.  So it would work like this or something like it

thread starts
thread executes code
thread waits a set amount of time AND can accept messages
while it waits it can accept a message to execute code now
If no such message comes then it waits for the set amount of time then executes the code and then waits again.
While it waits it can accept the execute code command at any time BUT does not reset the timer.  So it the timer is set for 5 minutes and the execute code message is recieve 5 seconds before the timer is up the code is executed and when the timer ends in 5 secs the code is executed again and the timer starts all over again

UNLESS the message to quit the thread is recieved.  Any time this message is recieved then the thread shuts down and free up an memory it was using.
0
simongod
Asked:
simongod
  • 5
  • 5
  • 3
  • +2
1 Solution
 
jhanceCommented:
Using Sleep() and receiving messages are fundamentally incompatible.

I'd suggest that you just set a TIMER in your thread for the interval you want to wait and then return to Windows.  If any messages come in, they will get processed and then when your timer expires, you'll get that message as well.

I'm of the opinion that NO Windows application should ever use Sleep().
0
 
h_p_softCommented:
Hi simongod.

Could you use the SetTimer/KillTimer API call? I think the code would look something like this:

{INITIALIZATION}

#define MSG_THREAD1 (WM_APP + 1)
#define MSG_THREAD2 (WM_APP + 2)

unsigned int iThreadTimer1, iThreadTimer2;

...
 
  iThreadTimer1 = SetTimer(hWnd, MSG_THREAD1, 5000, NULL);
  iThreadTimer2 = SetTimer(hWnd, MSG_THREAD2, 5000, NULL);

...

{WNDPROC}

  switch uMsg
  {
    ...
  case MSG_TIMER1:
  {
    // EXECUTE THREAD 1

    break;
  }

  case MSG_TIMER2:
  {
    // EXECUTE THREAD 2

    break;
  }
    ...
  }

...

{SHUT DOWN}

  KillTimer(hWnd, iThreadTimer1);
  KillTimer(hWnd, iThreadTimer2);

Then, the thread would recieve a MSG_THREADn message every five seconds, and would still recieve other messages. I don't know that this is the correct code - I'm not an experienced C++ programmer - but it could be a start.

Good Luck!

J.P.
0
 
simongodAuthor Commented:
I dont think using the Sleep function is the best choice either BUT I know no other way.  That is why I asked this question so that i could remove its use from my code.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jhanceCommented:
To repeat:

I'd suggest that you just set a TIMER in your thread for the interval you want to wait and then return
to Windows.  If any messages come in, they will get processed and then when your timer expires, you'll
get that message as well.
0
 
xzyeenaCommented:
"it the timer is set for 5 minutes and the execute code message is recieve 5 seconds before the timer
is up the code is executed and when the timer ends in 5 secs the code is executed again and the timer
starts all over again"


What if the message is recieved just before the timer ends and before the code is executed the timer goes off ... Is the code executed for the message exec command and when it returns checks if the timeer's ended then executes the code and resets the timer.

How is ur thread recieving messages ? what kinda messages ..are they events or the windows messages (on which u can use peekmessage)

You could use waitforsingleobject with the timeout value set to timer and waiting on the message event in a while loop.
and can have the following ..
if timeout :executecode
if message : then
              0 .note the current time : t0
              1. execute code
              2 timer = timer - (current time(t1) -t0)ms ;
              if (timer <= 0) execute code and set timer to TIMEOUT.

0,1,2 are so that the time spent in executing command is also included in the TIMEOUT value.

if you are using window messaging then the method is different.
Please let us know.

Xzyeena.
0
 
simongodAuthor Commented:
my thread is not recieving messages that is what i need (and the rest).  I am using Waitforsingleobject in a while loop like this

while (WaitForSingleObject(m_hEventKill, 0) == WAIT_TIMEOUT)

This while loop contains the code that needs to be executed.  at the end of the loop i have a sleep.  i dont want to do things this way.  how can it be changed to use the correct method?
0
 
jhanceCommented:
As long as you stay in your loop you will not receive any messages from Windows.  You must "return" and let Windows process the messages and dispatch them to your app.

Again, set a timer with SetTimer() and then return.  When the timer expires, you'll get a WM_TIMER message.  Either stop the timer with KillTimer or do whatever and return again.  If any other messages come for your window, you'll receive them....
0
 
xzyeenaCommented:
you can use  MsgWaitForMultipleObjects
For more info checkout msn help.

switch (ret = MsgWaitForMultipleObjects( NUM_EVENTS/* =1 */, m_heventkill, TIMEOUT, QS_ALLINPUT) )
{
case WAIT_TIMEOUT:
 execute code() ;
 break;
case WAIT_OBJECT_0 + 0:
 kill() ;
 break;
case WAIT_OBJECT_0 + NUM_EVENTS:
 while(PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
 {
    if (msg.message == WM_QUIT)
    {
     kill() ;
    }
   TranslateMessage(&msg);
   DispatchMessage(&msg);
// parse the message and do exec code();
 }
// set the timer
 break;
}

use this in a while loop.

Xzyeena.
0
 
bkdcCommented:
void stuff_proc(...)
{
 //that's the one that does your job

}

thread_proc(...)
{
  long conter;
  bool byebye;
 //WARNING
 //The timing is not very accurate  
 counter=5000;  //ms
 byebye=false;
 while(!byebye)
 {
   if(PeekMessage(...))
       {
         GetMessage(....);
         if (msg==ME_GETOUT)
         {
            byebye=true;
         }
         if (msg==ME_DOSTUFF)
         {
              stuff_proc(....);
         }
      }
   if(counter==0)
   {
     stuff_proc(....);
     counter=5000; //reset the counter;
   }
   else
   {
     Sleep(1);  //1 ms; quite impossible in Windows
                //so I would recommend at least 20ms
     counter--;
   }
}
q.e.d.
 That should be all; again - it's not very accurate but it might suit your needs
0
 
xzyeenaCommented:
You will have to modify the above code and there might be some syntax errors but  it should give u the basic idea.
u can also look at MsgWaitForMultipleObjectsEx.

Hope it helps
Xzyeena.
0
 
simongodAuthor Commented:
waitforsingleobject will work just fine i think.  What type of loop should eith er be in and what do i look for to quit the loop

If i use msgwaitformultipleobjectsex what loop should i put it in (i assume while) and what do i check for so that it quits the loop and the thread.
0
 
xzyeenaCommented:
u should check for the WM_QUIT or if ur m_heventkill is signaled then break out of the while loop and exit the thread.

if u use waitforsingleobject you will not be able to  know if u recieved a windows message or not.

repeating ...

while(TRUE)
{
switch (ret = MsgWaitForMultipleObjects( NUM_EVENTS/* =1 */, m_heventkill, TIMEOUT, QS_ALLINPUT) )
{
case WAIT_TIMEOUT:
execute code() ;
break;
case WAIT_OBJECT_0 + 0:
kill() ;
break;
case WAIT_OBJECT_0 + NUM_EVENTS:
while(PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
{
   if (msg.message == WM_QUIT)
   {
    kill() ;
   }
  TranslateMessage(&msg);
  DispatchMessage(&msg);
// parse the message and do exec code();
}
// set the timer
break;
}
}

write ur kill() so that u exit the thread.
0
 
simongodAuthor Commented:
I have a few questions about the code above

what will be either TRUE or FALSE here?

while(TRUE)

What is ret? and why is it used like this?  what is NUM_EVENTS? TIMEOUT is how long I want to wait for and it should be set to what for 30 secs?

switch (ret = MsgWaitForMultipleObjects( NUM_EVENTS/* =1 */, m_heventkill, TIMEOUT, QS_ALLINPUT) )
{
0
 
xzyeenaCommented:
while(TRUE) is an infinite loop.it will continue forever until and unless you break from the loop on certain condition.( calling kill() as in the above code).

TRUE is a hashdefined value set to 1.
You should not need to do it,it is system defined.
You can also use while(1).

ret is an integer which stores the return value of the WaitForSingleObject.
you could have the above in 2 statements too.
ret = WaitforSingleObject(...) ;
switch(ret)
{...
}

NUM_EVENTS = 1.

TIMEOUT is the value in millisec you want to wait before exceuting the code.(ur timer value in millisecond).

I did not undersatnd the last part of your question....
"set what for 30sec "

-Xzyeena.
0
 
simongodAuthor Commented:
with some modification i got this to work.  thanks for everyones help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now