《Windows核心编程》 [解决办法] The sleep() function shall cause the calling thread to be suspended from execution until either the number of realtime seconds specified by the argument seconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process. The suspension time may be longer than requested due to the scheduling of other activity by the system. [解决办法] sleep和Sleep不一样 [解决办法]
1570: VOID 1571: Sleep( 1572: DWORD dwMilliseconds 1573: ) 1574: 1575: /*++ 1576: 1577: Routine Description: 1578: 1579: The execution of the current thread can be delayed for a specified 1580: interval of time with the Sleep function. 1581: 1582: The Sleep function causes the current thread to enter a 1583: waiting state until the specified interval of time has passed. 1584: 1585: Arguments: 1586: 1587: dwMilliseconds - A time-out value that specifies the relative time, 1588: in milliseconds, over which the wait is to be completed. A 1589: timeout value of 0 specified that the wait is to timeout 1590: immediately. This allows an application to test an object to 1591: determine if it is in the signaled state. A timeout value of -1 1592: specifies an infinite timeout period. 1593: 1594: Return Value: 1595: 1596: None. 1597: 1598: --*/ 1599:
1600: { 1601: SleepEx(dwMilliseconds,FALSE); 1602: } 1603: 1604: DWORD 1605: APIENTRY 1606: SleepEx( 1607: DWORD dwMilliseconds, 1608: BOOL bAlertable 1609: ) 1610: 1611: /*++ 1612: 1613: Routine Description: 1614: 1615: The execution of the current thread can be delayed for a specified 1616: interval of time with the SleepEx function. 1617: 1618: The SleepEx function causes the current thread to enter a waiting 1619: state until the specified interval of time has passed. 1620: 1621: If the bAlertable parameter is FALSE, the only way the SleepEx 1622: returns is when the specified time interval has passed. If the 1623: bAlertable parameter is TRUE, then the SleepEx can return due to the 1624: expiration of the time interval (return value of 0), or because an 1625: I/O completion callback terminated the SleepEx early (return value 1626: of WAIT_IO_COMPLETION). 1627: 1628: Arguments: 1629: 1630: dwMilliseconds - A time-out value that specifies the relative time, 1631: in milliseconds, over which the wait is to be completed. A 1632: timeout value of 0 specified that the wait is to timeout 1633: immediately. A timeout value of -1 specifies an infinite 1634: timeout period. 1635: 1636: bAlertable - Supplies a flag that controls whether or not the 1637: SleepEx may terminate early due to an I/O completion callback. 1638: A value of TRUE allows this API to complete early due to an I/O 1639: completion callback. A value of FALSE will not allow I/O 1640: completion callbacks to terminate this call early. 1641: 1642: Return Value: 1643: 1644: 0 - The SleepEx terminated due to expiration of the time interval. 1645: 1646: WAIT_IO_COMPLETION - The SleepEx terminated due to one or more I/O 1647: completion callbacks. 1648: 1649: --*/ 1650: { 1651: LARGE_INTEGER TimeOut; 1652: PLARGE_INTEGER pTimeOut; 1653: NTSTATUS Status; 1654: 1655: pTimeOut = BaseFormatTimeOut(&TimeOut,dwMilliseconds); 1656: if (pTimeOut == NULL) { 1657: // 1658: // If Sleep( -1 ) then delay for the longest possible integer 1659: // relative to now. 1660: // 1661: 1662: TimeOut.LowPart = 0x0; 1663: TimeOut.HighPart = 0x80000000; 1664: pTimeOut = &TimeOut; 1665: }
1666: 1667: rewait: 1668: Status = NtDelayExecution( 1669: (BOOLEAN)bAlertable, 1670: pTimeOut 1671: ); 1672: if ( bAlertable && Status == STATUS_ALERTED ) { 1673: goto rewait; 1674: } 1675: return Status == STATUS_USER_APC ? WAIT_IO_COMPLETION : 0; 1676: }