Solved

class derived from CWinThread

Posted on 2001-08-23
15
576 Views
Last Modified: 2008-02-26
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
Comment
Question by:simongod
  • 5
  • 5
  • 3
  • +2
15 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6419212
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
 

Expert Comment

by:h_p_soft
ID: 6419235
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
 
LVL 2

Author Comment

by:simongod
ID: 6419647
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
 
LVL 32

Expert Comment

by:jhance
ID: 6419657
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
 

Expert Comment

by:xzyeena
ID: 6419739
"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
 
LVL 2

Author Comment

by:simongod
ID: 6419756
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
 
LVL 32

Expert Comment

by:jhance
ID: 6419793
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:xzyeena
ID: 6419903
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
 
LVL 2

Accepted Solution

by:
bkdc earned 200 total points
ID: 6419906
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
 

Expert Comment

by:xzyeena
ID: 6419918
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
 
LVL 2

Author Comment

by:simongod
ID: 6419940
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
 

Expert Comment

by:xzyeena
ID: 6419975
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
 
LVL 2

Author Comment

by:simongod
ID: 6422909
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
 

Expert Comment

by:xzyeena
ID: 6423184
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
 
LVL 2

Author Comment

by:simongod
ID: 6423393
with some modification i got this to work.  thanks for everyones help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now