Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sleep(), problems using it

Status
Not open for further replies.

nyjil

Programmer
Jul 14, 2000
32
US
The idea of the Sleep() function is to cause the program to pause a moment, then resume the program. As I already know, the parameter of the Sleep function is the milliseconds that the program is suppose to pause for. What I'm having problems with is it's appropiate use. Let me give you an example. A function assigns a value to a CString variable such as this:

CString theVariable;

void MyProgram::Assign()
{
theVariable = "Hello World"
UpdateData(FALSE);

etc, etc, etc.
}


Thus, assuming that theVariable is assigned to an edit box, the words 'Hello World' would appear in the box. The problem I'm having is this:

CString theVariable;

void MyProgram::Assign()
{
theVariable = "Hello World"
UpdateData(FALSE);
Sleep(5000);

etc, etc, etc.
}


In this example, even though the Sleep command comes after UpdateData(FALSE), the program will still wait 5 seconds before printing the 'Hello World' in the edit box. What I'm wanting is to print 'Hello World' in the edit box and then wait 5 seconds, then continue with other commands in the program. Am I doing something wrong? I must be. Is there an easier way to accomplish the same thing? Any help in this matter would be appreciated.

Thanks
Nyjil
 
The Sleep function suspends the execution of the current thread for the specified interval.So would work same even if you keep it before UpdateData or after it
I am still not getting why do you want a delay in that ...if you want delay due to some other thread's processing then you might use some threading model for that instead of relying on Sleep. So please see it to it if you can void usage of Sleep....

 
Hi, the reason it's waiting to update the edit box is because your UpdateData() function is simply posting a WM_PAINT message or something to the edit box. By the time this message is ready to be processed, your code has already moved onto the Sleep() function thereby locking it up. You could perhaps use RedrawWindow() (I think it is) on your edit box to update it's contents immediately.

Also, like panka said, try to avoid using the Sleep() function in anything other than a separate thread. Perhaps SetTimer() would be better - that way, it wouldn't lock up your computer.
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Thanks for the ideas. The SetTimer() idea seems to be a good one. Think I'll go from there. Thank you.

Nyjil :)
 
qednick:
> ... it wouldn't lock up your computer
Do you think, Sleep() stops execution of all processes?
 
No, I was using that in the general sense of a single thread - it actually locks up the executing thread - if the program in question has one main thread, it will appear that the program has locked up. Excuse me for not being precise to the 'T'
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
qednick is correct about the WM_PAINT message processing. Try using:
pEdit = reinterpret_cast<CEdit*>(GetDlgItem(<DLG_ITEM_ID>));
pEdit->SetWindowText(theVariable);
where <DLG_ITEM_ID> is the Id assigned to the edit control.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top