Click here to Skip to main content
15,896,526 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to make XSleep()function continue execution untill i click on the button.

Is there any other funtion that sleep untill an event occurs?


when XSleep is used XSleep(1000);
i stops execution for 1000 milliseconds.


Instead of giving a value the Execution should stop for time untill I do OnClick operstion on a button.
C++
void CDemoDlg::OnBnClickedSongPlay2()
{
	
if(PauseFlag==true)
{
	int i=0;bool b=true;
	while(b)
	{
		i++;
		XSleep(i++);
// some code to be written for any event occurs so i can make b=false;
		
	}

		
	}
	else
	{
		PauseFlag=true;
	return;
	}
}



In the above code it pauses and never resume please correct this code to resume it.
Posted
Updated 14-Mar-12 2:15am
v4

1 solution

Using sleep() should be avoided. I did not had a look at the XSleep() source, but it seems to avoid some of the disadvantages of the sleep function.

The usual way is to use WaitForSingleObject() with an event. If you tell us about the context where you want to use sleep(), we may find a solution to avoid it.

[UPDATE according to update question]
One solution may be terminating the thread created by XSleep(). This requires modification of the XSleep() source by adding code to terminate upon a specific message (e.g. the BN_CLICKED notfication of your button).

A better solution would be a proper design. I understand that you are playing audio and want to provide a pause and resume option. Then stop playing upon pause, and save the actual play position. Upon resume, start playing at the stored position. Such a design avoids the usage of sleep commands.
[UPDATE]
 
Share this answer
 
v2
Comments
chaiein 15-Mar-12 23:55pm    
for file type of CStdioFile to find the position where it stops getPositon() is used but the seek fails during execution. the getPosition is giving a wrong value.
Jochen Arndt 16-Mar-12 3:45am    
Without knowing what you are doing, I can't help you.
chaiein 16-Mar-12 5:05am    
I am reading each character from text file and displaying the letters when i click on button it should stop there and when again when i click that button it should continue from where it paused.
Jochen Arndt 16-Mar-12 5:26am    
OK. To catch events during such operations, the professional solution is to use a thread for reading.
But you may use a simple solution here: From within the pause button handler, set the PauseFlag member variable, and from within the resume button handler clear it. You should also add a stop variable as member var that is set when the stop / cancel button is activated. The XSleep() function will process messages like the BN_CLICKED notifications from the buttons. The main loop may then look like this:
while (!bStop)
{
if (!bPauseFlag)
ProcessChar();
XSleep(DELAY_TIME);
}
EndDialog(IDCANCEL);
This requires that OnCancel() and OnOK() does not close the dialog immediately when playing is active:
void CMyDialog()::OnCancel()
{
if (bIsPlaying)
bStop = true;
else
CDialog::OnCancel();
}
chaiein 16-Mar-12 6:29am    
ProcessChar() has the ReadFileTxt() which gives me warning!!

ReadFileTxt every time called will open the file when i give pause it exits with out closing and when i click pause again and goes to else loop(placed resume code)when ever file opens it starts from first character. First of all it tries to open gives a warning because its already open.


And what does DELAY_TIME contain?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900