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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-6-27 00:03:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
方法如下. C' m6 [9 I  G# Z/ B/ {6 q
建立 xsleep.cpp和xsleep.p文件# g/ k5 s$ y$ }) m% e9 v1 `
xsleep.cpp文件代码如下
! b; ]" l4 x5 A6 n1 S) B) Z
  1. //Download by http://www.cncml.com: h( @& }! y1 s
  2. #include <windows.h>
    8 p5 ?, B7 E( `- y: _9 e
  3. #include <stdafx.h>+ F" [' S# X% H2 |, N" y( s

  4. - \: k. b: R' C2 L$ v/ x, c
  5. // This structure is used internally by the XSleep function
    . G: a; {3 q8 G! q2 ?2 k8 s9 b
  6. struct XSleep_Structure
    . B' h' ~/ T: S) C( O% ?% V
  7. {4 Z/ I+ x. ?, N6 v
  8.         int duration;9 Y6 K9 ^+ F( b% @" B1 G
  9.         HANDLE eventHandle;& R0 S+ X9 k8 V8 w2 m' Z
  10. };8 D' e/ a- [- P" |; D' x  a8 K8 i! V

  11. 2 w2 N/ y# y% ~! W& [; t
  12. 9 A/ s3 V9 ~8 t. B) _7 L
  13. //////////////////////////////////////////////////////////////////////6 e2 n4 s1 l2 L4 @4 N, K# h6 w( W
  14. // Function  : XSleepThread()" _* a, @% u6 ?0 }$ _
  15. // Purpose   : The thread which will sleep for the given duration+ i1 o' `# v. ]7 j0 H5 l- R! d
  16. // Returns   : DWORD WINAPI
    ; ]8 I$ V( R8 j. }8 V7 q: d
  17. // Parameters:      
    8 F* S& J) q& Y. n  x3 @& e
  18. //  1. pWaitTime -
    # l  b! j/ m( z3 i
  19. //////////////////////////////////////////////////////////////////////
    ; x1 l; M/ y# i5 r
  20. DWORD WINAPI XSleepThread(LPVOID pWaitTime)
    ) t, h# \# y* p/ a6 ^; W( z% w& I
  21. {
    ' h' v, y' v% H+ K# p7 o7 @/ l# z
  22.         XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime;! p4 p/ \- G7 A; _5 d, j

  23. ( y% v+ G9 c* p6 \$ V# P4 V- @5 w
  24.         Sleep(sleep->duration);
    ! w) E$ {) k+ A; \) F
  25.         SetEvent(sleep->eventHandle);5 h% a9 H: F" p8 K# L9 Q3 Q
  26. 3 x, H% U# Q/ [* Z. m( Q* m
  27.         return 0;
    / L0 S- T: {8 E2 x4 e
  28. }% ~- I2 j4 r& D7 `: e7 s
  29. ' U' e  [0 J9 B5 V& u; S" d
  30. //////////////////////////////////////////////////////////////////////
    ) e8 C, ]& `$ t) A
  31. // Function  : XSleep()
    2 h0 S, |1 X; x, f. m# a- W
  32. // Purpose   : To make the application sleep for the specified time4 [& R2 G5 o" }; ?0 A' T
  33. //             duration.
    * ?" u1 V$ ?, C* b8 o% A; v
  34. //             Duration the entire time duration XSleep sleeps, it
    % @5 {/ i7 {) d# W& d
  35. //             keeps processing the message pump, to ensure that all5 L% H2 u9 O7 Y9 Q6 ~
  36. //             messages are posted and that the calling thread does$ M4 v6 J0 I) X% M/ o
  37. //             not appear to block all threads!
    ! n: z9 R5 Q4 |& q2 R! Y2 |
  38. // Returns   : none( G* X# |/ o3 v9 J6 `8 W
  39. // Parameters:       / l4 Q; ^1 G8 i. W$ j+ G
  40. //  1. nWaitInMSecs - Duration to sleep specified in miliseconds.
    * Y3 V' W& ~6 h! G
  41. //////////////////////////////////////////////////////////////////////
    4 |* q6 D7 {. [+ e- \' r/ A! b
  42. void XSleep(int nWaitInMSecs,int nAscll)
    ! g8 M% J  u6 t: `9 A
  43. {+ e# l  I: T! i1 ^) J" ~+ \
  44.                 % _  i- x& Q; B+ s0 g8 M4 i- h# ~
  45. <blockquote><span class="Apple-tab-span" style="white-space:pre">        </span>INPUT input[2];
复制代码

: z7 J, J4 M0 r6 S) b- D, g
% x* b6 D$ Z$ U/ X- |xsleep.h文件代码3 ^. O5 u& k% S$ g( M: [; M  }9 r6 G
. A2 G; ^' Y2 d
  1. //Download by http://www.cncml.com8 J: Q) J/ z, c  [1 w# _& J( W
  2. #ifndef _XSLEEP_H_, J8 q# w$ @: z& r! H
  3. #define _XSLEEP_H_2 ~2 ~/ \( W+ ?

  4. 2 |, |3 ]8 B8 [! A  i) v6 E
  5. void XSleep(int nWaitInMSecs, int nAscll);+ x  R5 Y* p6 y

  6. ' p# Y/ {5 {* ]0 g) e6 o+ H" M+ V
  7. #endif // _XSLEEP_H_
    2 L) _: l' L5 u8 t  t
复制代码
0 a; ^( c1 y) c; D. X

: z  L1 m3 r9 {: y: d& N' @mfc中的调用代码如下3 `; ^' }+ `$ Z3 y4 F. S  b( z1 Y
  1. int ascll;
复制代码
  1. XSleep(500,ascll);
复制代码

  r8 v1 \! [' X5 I/ _$ g2 x
3 I+ j" A% G3 }$ H, Q
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-17 20:21 , Processed in 0.078495 second(s), 19 queries .

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