Threads
-
Hi, I have a multi-threaded application and have the following problem, which I am not sure how to solve. I have 2 threads running. The 1 thread I would like to SUSPEND or place in SLEEP mode. This is so that I can give the processor a chance to do other things. This thread I only need to work every half an hour. If I place the thread in Sleep, I cannot get it out of that mode until the Sleep ends. Also if I should need to kill the thread and it is in sleep, I am going to have to wait a half an hour ( at the most) before the thread dies. How do I manage this? Thanks in advance for any ideas. Regards Rui
-
Hi, I have a multi-threaded application and have the following problem, which I am not sure how to solve. I have 2 threads running. The 1 thread I would like to SUSPEND or place in SLEEP mode. This is so that I can give the processor a chance to do other things. This thread I only need to work every half an hour. If I place the thread in Sleep, I cannot get it out of that mode until the Sleep ends. Also if I should need to kill the thread and it is in sleep, I am going to have to wait a half an hour ( at the most) before the thread dies. How do I manage this? Thanks in advance for any ideas. Regards Rui
Ruca wrote: If I place the thread in Sleep, I cannot get it out of that mode until the Sleep ends. You shouldn't be using Sleep for this. You have three choices: 1. The most complicated, but more elegant and powerful: Use WaitForSingleObject and use an event object with the timeout value being the Sleep period 2. I do not recommend, but easier: Use SuspendThread from the thread 2 for suspending thread 1. Then set some bool and ResumeThread for resuming the execution. 3. The most horrible: If the program is terminating, you can use TerminateThread My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool
-
You could also use a timer catching the WM_TIMER message. To make the trade terminate, send it the WM_QUIT message, which it will also catch. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
- TreeBeardHe's using Sleep, so he's not processing messages on his thread's message pump... My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool
-
Ruca wrote: If I place the thread in Sleep, I cannot get it out of that mode until the Sleep ends. You shouldn't be using Sleep for this. You have three choices: 1. The most complicated, but more elegant and powerful: Use WaitForSingleObject and use an event object with the timeout value being the Sleep period 2. I do not recommend, but easier: Use SuspendThread from the thread 2 for suspending thread 1. Then set some bool and ResumeThread for resuming the execution. 3. The most horrible: If the program is terminating, you can use TerminateThread My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool
You could also use a timer catching the WM_TIMER message. To make the trade terminate, send it the WM_QUIT message, which it will also catch. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
- TreeBeard -
He's using Sleep, so he's not processing messages on his thread's message pump... My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool
The suggestion is NOT to use Sleep, because it is blocking, so you are unable to tell the thread to cleanup and quit. The use of either
WaitForSingleObject
with a 1800000 milliseconds time-out orSetTimer
and catching the WM_TIMER and WM_QUIT messages is better. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
- TreeBeard