您尚未登录,请登录后浏览更多内容! 登录 | 立即注册

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13435|回复: 0
打印 上一主题 下一主题

[C++学习资料] 非滞后式延迟执行

[复制链接]
跳转到指定楼层
楼主
发表于 2018-6-27 00:03:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
方法如下
5 h) D5 ~3 [; ]0 W9 C% n2 z$ c建立 xsleep.cpp和xsleep.p文件7 y  {- V" J; C' g/ u" D; O
xsleep.cpp文件代码如下( V1 N( l5 L/ h
  1. //Download by http://www.cncml.com
    + D% y( F; F% O1 T  I& r! [4 N& |
  2. #include <windows.h>9 `& C4 |% ~4 n2 ?: q, A
  3. #include <stdafx.h>' ~! {0 \5 b  p2 g1 }
  4. & b$ L- {$ I9 X& ?% O
  5. // This structure is used internally by the XSleep function ' w% D4 Q. c$ l9 E' d/ x1 ^  S
  6. struct XSleep_Structure$ M2 V9 d0 L% a0 U& R* ~. [- P% F; M
  7. {0 h% m3 b; e' a9 ~: m4 N% i
  8.         int duration;/ H! f; q. |" C+ D5 @
  9.         HANDLE eventHandle;
    5 b. z* m# u8 |% q
  10. };
    / }0 u9 ^/ |) T% G( X% J# t

  11. 8 E) V* r7 A/ B8 S

  12. . g  R9 m% x. V
  13. //////////////////////////////////////////////////////////////////////! K- A9 W* B- C$ Z+ @- Z4 M
  14. // Function  : XSleepThread()1 C5 `0 h6 u  E5 T
  15. // Purpose   : The thread which will sleep for the given duration
    . E3 U7 y" _. c! k
  16. // Returns   : DWORD WINAPI2 Y8 o# D  {0 h/ x8 I8 n
  17. // Parameters:      
    $ E) t3 d& P7 m9 O: @1 s1 T! P0 m
  18. //  1. pWaitTime -+ M  X( j/ r& m3 ?8 x
  19. //////////////////////////////////////////////////////////////////////. w$ l6 }( o* u: q: K
  20. DWORD WINAPI XSleepThread(LPVOID pWaitTime)& X) ^  b% R# S! s+ _8 t( J
  21. {
    4 o: _/ o: }7 @! ~
  22.         XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime;1 C! Z8 c" h  \. n5 o8 M  y5 T
  23. + k- G1 ]$ ]! `
  24.         Sleep(sleep->duration);+ v& ?  _! u$ I8 b/ I# J
  25.         SetEvent(sleep->eventHandle);
    2 d8 ?5 A6 T# _6 u9 d+ H

  26. ( M* O+ \) f* F* ]/ i  A
  27.         return 0;
    # L$ S* u' N5 n1 q. d
  28. }, J- Z0 Q, [2 [- j" P- t* \

  29. 6 K0 F+ r5 ?# m; ^" f! ~
  30. //////////////////////////////////////////////////////////////////////
    # Z/ I  B. A' }; @) `& L# }
  31. // Function  : XSleep()* M9 j3 t" W* N% f  O( m7 z- F
  32. // Purpose   : To make the application sleep for the specified time1 I  y( w  `0 R+ S
  33. //             duration.
    2 e5 n; H+ l7 O) R1 G# f* v* L
  34. //             Duration the entire time duration XSleep sleeps, it5 g* c* Z- k# {6 X9 n
  35. //             keeps processing the message pump, to ensure that all
    ! t- O7 Q* b1 f
  36. //             messages are posted and that the calling thread does; y- f; p& W2 ], r3 J  e6 v
  37. //             not appear to block all threads!) U, c0 Z& F; ~
  38. // Returns   : none# x/ @) E) a0 A' C; t! M
  39. // Parameters:       5 h& g( ]) p' H' h
  40. //  1. nWaitInMSecs - Duration to sleep specified in miliseconds.
    & K' a5 N; y- `  \: b) T: L8 v, [
  41. //////////////////////////////////////////////////////////////////////) [7 Q" _& D. b
  42. void XSleep(int nWaitInMSecs,int nAscll)
    + A* U1 f4 ?, f, |. Y6 w, R# n( _
  43. {
    & V$ a' s  j7 y
  44.                
    " k/ d3 p; i* o5 V" y6 m
  45. <blockquote><span class="Apple-tab-span" style="white-space:pre">        </span>INPUT input[2];
复制代码

  f) m/ w2 n( U* b2 y& m- G# j9 ^4 y/ c" C  S- n) g
xsleep.h文件代码
) H# T6 S% e, _0 C0 d% b$ R/ G; ~% O+ u& G
  1. //Download by http://www.cncml.com
    1 }' d% S  a2 `8 R/ H
  2. #ifndef _XSLEEP_H_/ P" B& a! ]) J5 S* d( j* L
  3. #define _XSLEEP_H_7 }$ l6 D% Y* U) [1 q
  4. - g8 ]- N) y' p* t
  5. void XSleep(int nWaitInMSecs, int nAscll);
    % ]2 i% x8 K) r+ ?% j/ t; _7 e5 |

  6. * r, I. r+ ~6 E* v: y
  7. #endif // _XSLEEP_H_
    0 V- F+ ]$ N- N( k( G# n2 G
复制代码
- F, ^" B  d2 x' o) L+ A
  y0 {* k0 p7 g, c! V
mfc中的调用代码如下
3 u0 ~+ i4 S/ _' ]  z) z
  1. int ascll;
复制代码
  1. XSleep(500,ascll);
复制代码
6 V  K* J% s  `' M: p$ w& r1 |+ B

  {$ C  e, O+ E# M1 ]" h- `
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 15:55 , Processed in 0.060204 second(s), 20 queries .

Copyright © 2001-2026 Powered by cncml! X3.2. Theme By cncml!