|
Read up on threads here and here.
anilksingh wrote: I tried Sleep...
Using Sleep() with threads is usually a bad idea.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
one thing that you can do is that create a static flag=false as a global variable.
now in the loop you can check that if the flag is set to true.
(You will set the flag to true when the second button is pressed.)
if the flag is set simply put return 0; in the block of code where you want the thread function to end.
Here's some code for you that is inside the thread function :
CMyDlg *test=(CMyDlg *)pVoid;
for(int i=0;i<1000;i++)
{
Sleep(10);
if(flag==true)
{
CString s;
s.Format ("%d",i);
AfxMessageBox("Thread stoping on "+ s);
return 0;
}
else
{
CString s;
s.Format ("%d",i);
test->m_number.SetWindowText (s);
}
}
return 0;
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
-- modified at 9:34 Tuesday 13th June, 2006
|
|
|
|
|
put the shared resource in crtical section (global varaible)dont use sleep
never say die
|
|
|
|
|
It was just to show the user. Afterall this was not a live example.
It was just to give the user the time to perform the inputs while the thread is running
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
The old way (Win16) of doing this was to pump messages in the loop so that the other button will respond to user input. Today, a worker thread would be what I would suggest.
Look up how to create a background thread using the links provided in the previous answer. While you can use a shared variable to terminate the thread, I would suggest looking into how "event" objects work with threads to get a feel for them and how they should be used.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
Create an event handle (using CreateEvent). Pass that handle to the loop when you create it (its quite common to pass such objects in the LPARAM pointer used when creating a thread). When you need to terminate the thread, call SetEvent. Internally to the loop, you will use WaitForSingleObject with an appropriate timeout value (in your case, 0 would probably work since you just want to check to make sure it has already been set). If WaitForSingleObject returns WAIT_OBJECT_0, then your event was signaled and you should stop the loop.
Alternatively, you could create a boolean flag and put a critical section around every access. This works just as well, but is error prone (one access without a critical section breaks the entire system). The Event solution doesn't have that problem.
If you use Sleep in any application, you can pretty much be assured that your design is flawed. Using it in Multithreaded applications to try to manage synchronization between threads is worse than useless. Use proper thread synchronization techniques and you won't have issues.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: Alternatively, you could create a boolean flag and put a critical section around every access. This works just as well, but is error prone (one access without a critical section breaks the entire system). The Event solution doesn't have that problem.
You can also go the shared variable route and use things like InterlockedCompareExchange(...) (CMPXCHG ) as well. One thing though - for this particular kind of situation, I am not sure that synchronized access is required. Since the value is not being used as a counter, or anything like that but just a simple flag, it should be OK to have one thread reading it waiting for its value to change while another threads writes it (once) to set that value. I do not think that the reading of the value will interfere with the writing of it, and the thread would catch the change on the next read. (Assuming automatic volatile handling and things like that.)
And yes, that was my 5.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
As your thread title says, you should go for multithreading rather than the sleeping business. But what's the nature of the loop? can it be done with a Timer? I used to create threads unnecessarily when it could be done with timers . So see to that it cannot be done with a timer first then go for multithreading. As said by David, never get your Sleep inside your thread or any loop, it puts the entire application to sleep
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
|
|
|
|
|
(Assuming you are talking about Sleep(...) ...)
VuNic wrote: As said by David, never get your Sleep inside your thread or any loop, it puts the entire application to sleep
No, it just the thread it is called on... An application may consist of multiple threads. Sleep(...) only affects one particular thread.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
Thanks james, I thought Sleep is global call. hmm.. thanks for the info.. nice update for me.
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
|
|
|
|
|
Hi all,
i want to change the color of a button when the cursor comes into contact with it...can anyone help me out in this???
Thanks in advance..
Rajeev
-- modified at 9:09 Tuesday 13th June, 2006
|
|
|
|
|
rajeev82 wrote: Hi all,
i want to change the color of a button when the cursor comes into contact with it...can anyone help me out in this???
Here[^] is the good article on Bitmap buttons
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
assume that I have some project that can support hot key and this project is running.Then I open another program for doing something.
While I'm using this program the first program will can't use hots key because now the focus is on the program that I'm using it.
If I want to make it always support hot key although I use another program.
How I do for this case?
|
|
|
|
|
|
Max++ wrote: assume that I have some project that can support hot key...
How are you doing this?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I want to create bitmaps and save them on the hard disk.
May be i can start with 1.bmp, 2.bmp.... so on.
Basically on one webpage there is slideshow.
i want to find this window of webpage ,
I want to initiate a timer (same as the webpage interval at which slide changes) , at this timer event I want to capture the slide or web window as bitmap and save it on harddisk at any path.
How to find such a window. (internet web page)
How to create and save bitmap in the above manner.
Any hints?
thanks
row
|
|
|
|
|
|
Have a look to this :
CLick Me[^]
It will help you in Capturing a Window Image Into a Bitmap File
Cheers
"Peace of mind through Technology"
|
|
|
|
|
Dear all
I want to implement a watchdog in my code ?
please give me some example code.
Thanks a lot
|
|
|
|
|
wrote: I want to implement a watchdog in my code ?
Here[^] is the code in dotnet which may helps you.
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
|
WhiteSky wrote: but its C#
Already specified that code of dotnet.
May get any idea from same.
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
I hope
whitesky
|
|
|
|
|
Me too
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
And what is your watchog supposed to do exactly ? And what is the 'resolution' you want to achieve ? (remember that windows is not a real-time OS).
Cédric Moonen
Software developer
Charting control
|
|
|
|