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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pausing a program 1

Status
Not open for further replies.

commtalonn

Programmer
Aug 9, 2001
7
US
How do you pause your program? I've used the functions Pause() and Delay() that I saw somewhere, and the Sleep() function, which is more widely known, but none of them pause my program. It just rips right through the function and ignores it. It compiles w/no errors but it doesn't work! arrrggghhhhh, got any advice?!!
 
is your program multithreaded? John Fill
1c.bmp


ivfmd@mail.md
 
I don't know what you mean by multithreaded... My program is simple. It creates a Winsock socket, connects to another computer, sends data, then exits. It's all in one source file; it's just a few hundred lines of code. Do you know why the function doesn't work?
 
Thread is an execution. Multiple threads are paralel executions, as example multitasking in Windows, multiple programs runs simultaneously. Each program(ie process) has at least one thread. It can have many in one single process, why not? To create a thread just CreateThread(...)

because theese ones stops only the current thread. On other threads they don't make anything. See in which thread you want to put it. John Fill
1c.bmp


ivfmd@mail.md
 
Thread is an execution. Multiple threads are paralel executions, as example multitasking in Windows, multiple programs runs simultaneously. Each program(ie process) has at least one thread. It can have many in one single process, why not? To create a thread just CreateThread(...)
The pause function doesn't work usualy because they stops only the current thread. On other threads they don't make anything. See in which thread you want to put it. John Fill
1c.bmp


ivfmd@mail.md
 
I don't understand the relevance to my topic.... I appreciate your effort but I guess the confusion is on my end. I don't use threads, it's just a simple c++ proggie.
 
see if some objects work with different threads. Usualy sockets are associated to different threads. In MFC CWnd is also often associated to different threads. John Fill
1c.bmp


ivfmd@mail.md
 

Sleep() must not be called or you have a very low value so it's pausing for a very short time. Don't bother with multi-threading if you don't need it but it's something you will need if you plan to improve on your programming.

Make sure Sleep() is not in an if(..) statment or some conditial statment. Also remember that it's milliseconds you pass in not seconds.

If you're using sockets and you are asking this because you want to wait to receive data then make the socket you're receiving on blocking and set the time out option instead. Much better way to do things.

Bother C
 
Thanks, brotherC, but the function Sleep() is being called, and not only am I using milliseconds, but I started using extravagantly high numbers of milliseconds (i.e. 500000) to make sure it isn't pausing. Sure enough, it isn't working. Dang Microsoft products.... >_<
 
Could you post the offending section of code? That might allow us to have a look at your problem, and perhaps identify potential problems.
 
I think I found out what's wrong. The program consists of one function, main(). Every Sleep() is called inside main... and it collects every value of Sleep() I've put inside main() and pauses for that amount of time before running the program. I used cout << debug tags to find this out. Here's pretty much the layout of my program:

main(blahblah)
{
....
cout << &quot;Pause now!\n&quot;;
Sleep(10000);
cout << &quot;10 seconds have passed\n&quot;;
....
cout << &quot;Pausing again\n&quot;;
Sleep(10000);
cout << &quot;10 seconds have passed again.\n&quot;;
....
}

The output of my program is that it will pause for 20 seconds, then run the rest of the program. What's causing this? Thanks for the help!
 
you should flush streams:


main(blahblah)
{
....
cout << &quot;Pause now!\n&quot;<<flush;
Sleep(10000);
cout << &quot;10 seconds have passed\n&quot;<<flush;
....
cout << &quot;Pausing again\n&quot;<<flush;
Sleep(10000);
cout << &quot;10 seconds have passed again.\n&quot;<<flush;
....
} John Fill
1c.bmp


ivfmd@mail.md
 
That did it!! All I needed was to throw in that <<flush; at the end of my cout<<. Yay! Thanks, John, and everyone else that helped!
 
I'm having trouble using the Sleep() command myself. I am trying to right a very basic DOS program, when I enter Sleep(100); after a specific line, I get an error telling me Sleep is an undeclared identifier. Which header file do I need for this to run? and am I using it right? Thanks..
 
I believe Sleep() is a Win API call because it is defined in Winbase.h...it doesn't exist in a DOS based program. I'm not sure what you could us in the DOS environment...you may want to try just a simple empty loop. Kind of crude, but gets the job done.

Niky Williams
Lead Engineer
NTS Marketing, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top