[comp.windows.ms] sleep

papa@pollux.usc.edu (Marco Papa) (11/29/89)

Does anybody have a "quick and dirty" UNIX-like sleep() for Windows?
If that is not the case, what would be the best way to do it? I.e.:

1. Using the timer device
2. Using the timer interrupt
3. Using the time() routines of MS-C

Using 1, would allow an implementation that is more "friendly" to other
concurrent tasks.

-- Marco
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
uucp:...!pollux!papa       BIX:papa       ARPAnet:pollux!papa@oberon.usc.edu
"There's Alpha, Beta, Gamma, Diga and Caligari!" -- Rick Unland
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

dan@rna.UUCP (Dan Ts'o) (12/01/89)

In article <21446@usc.edu> papa@pollux.usc.edu (Marco Papa) writes:
>Does anybody have a "quick and dirty" UNIX-like sleep() for Windows?
>If that is not the case, what would be the best way to do it? I.e.:
>
>1. Using the timer device
>2. Using the timer interrupt
>3. Using the time() routines of MS-C

>Using 1, would allow an implementation that is more "friendly" to other
>concurrent tasks.

	Below is my quick and real-dirty sleep() which I use in regular
MSC. Love to see a better one that really is friendlier and have it
really make a difference. Most DOS programs seem to busy-wait anyways.

#include <time.h>

sleep(n)
register unsigned n;
{
	register int i;
	long tm, tm1;

	time(&tm);
	tm1 = tm + n;
	while (n--)
		for (i = 0; i < 30000; i++) {
			time(&tm);
			if (tm >= tm1)
				return 0;
		}
	return -1;
}

/* Millisecond sleep */
nap(n)
long n;
{
	register int i;
	long tm, tm1;

	tm = clock();
	if (tm == -1) {
		fprintf(stderr, "Clock() cannot return ticks\n");
		return -1;
	}
	tm1 = tm + n;
	while (n--)
		for (i = 0; i < 30000; i++) {
			tm = clock();
			if (tm >= tm1)
				return 0;
		}
	return -1;
}
#endif

mguyott@mirror.UUCP (Marc Guyott) (12/02/89)

>In article <21446@usc.edu> papa@pollux.usc.edu (Marco Papa) writes:
>>Does anybody have a "quick and dirty" UNIX-like sleep() for Windows?

This is one possible solution:

----
#define TIMER_ID  1
#define EXIT_CODE 0x4000

void FAR PASCAL Sleep(hWnd, wMilliSeconds)
    HWND hWnd;
    WORD wMilliSeconds;
{
    MSG Msg;

    if (!SetTimer(hWnd, TIMER_ID, wMilliSeconds, NULL))
    {
        MessageBox(GetActiveWindow(),
                   "A call to SetTimer() in Sleep() failed.",
                   "Fatal Error!", MB_SYSTEMMODAL | MB_OK);
        FatalExit(EXIT_CODE);
    }

    GetMessage(&Msg, hWnd, WM_TIMER, WM_TIMER);

    if (!KillTimer(hWnd, TIMER_ID))
    {
        MessageBox(GetActiveWindow(),
                   "A call to KillTimer() in Sleep() failed.",
                   "Fatal Error!", MB_SYSTEMMODAL | MB_OK);
        FatalExit(EXIT_CODE + 1);
    }

    return;
}
----
	      "All my life I always wanted to BE somebody.
	       I see now I should have been more specific."
			     Jane Wagner
Marc Guyott					    mguyott@mirror.tmc.com
{mit-eddie, pyramid, harvard!wjh12, xait}!mirror!mguyott
Mirror Systems		 Cambridge, MA	02140	       617/661-0777

papa@pollux.usc.edu (Marco Papa) (12/02/89)

In article <865@rna.UUCP> dan@rna.UUCP (The Dan of all vices) writes:
|In article <21446@usc.edu> papa@pollux.usc.edu (Marco Papa) writes:
||Does anybody have a "quick and dirty" UNIX-like sleep() for Windows?
||If that is not the case, what would be the best way to do it? I.e.:
||1. Using the timer device
|
|	Below is my quick and real-dirty sleep() which I use in regular
|MSC. Love to see a better one that really is friendlier and have it
|really make a difference. Most DOS programs seem to busy-wait anyways.

Busy-wait is a no-no in MS-Windows.  I've already done it using technique
no. 1 and it works like a champ.  If I have some time, maybe I'll post it.

-- Marco
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
uucp:...!pollux!papa       BIX:papa       ARPAnet:pollux!papa@oberon.usc.edu
"There's Alpha, Beta, Gamma, Diga and Caligari!" -- Rick Unland
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=