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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-6-27 00:03:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
方法如下. l' L1 ]3 }8 _5 t$ D5 ^
建立 xsleep.cpp和xsleep.p文件
5 T; `+ W$ G$ z' _% Dxsleep.cpp文件代码如下! W, c% S- L1 N1 L3 W
  1. //Download by http://www.cncml.com
    ! U2 F. G) u- p2 f1 X
  2. #include <windows.h>' g+ L8 r: G$ E! Z
  3. #include <stdafx.h>) s) \$ `% ]) s0 W" t8 y
  4. ) l% T5 i9 |! e- T, i0 f
  5. // This structure is used internally by the XSleep function
    , `8 u# U  d' u. f$ B' s% L9 j
  6. struct XSleep_Structure
    7 X8 p0 m' U2 M# g9 w+ s
  7. {4 l! w2 J/ w6 j( @/ J1 J
  8.         int duration;
    8 Y6 e0 R" F; \  C% H! g4 B
  9.         HANDLE eventHandle;3 O" x6 r% a! g% L( Q
  10. };; k& W4 g4 y" P9 G
  11. ) Y1 h& d+ K* h6 T' c3 h2 p) ~

  12. . o' K1 B# g2 }6 s" Z
  13. //////////////////////////////////////////////////////////////////////
    0 ^, y% e- x1 }8 H: i0 J' F, A1 C1 D9 k
  14. // Function  : XSleepThread()
    * E2 `* v( @1 e* q+ E
  15. // Purpose   : The thread which will sleep for the given duration
    & P  K* j0 S+ W" t
  16. // Returns   : DWORD WINAPI: Q$ D: d* z& `2 I) \0 D  g
  17. // Parameters:       " a9 h+ E8 h0 S: R( }1 v; U% N
  18. //  1. pWaitTime -8 G# ?4 V( K: f
  19. //////////////////////////////////////////////////////////////////////* s( ^/ q, C' Q
  20. DWORD WINAPI XSleepThread(LPVOID pWaitTime)* ]# d7 q$ ~" H3 ?
  21. {( u4 f9 V% j! ]! v9 I
  22.         XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime;
    2 t( U3 R5 A* ]2 c$ c$ V7 {
  23. 0 g9 g9 C: W. _3 S3 \8 t9 D6 D
  24.         Sleep(sleep->duration);; w9 ?6 w' E1 {/ ]/ s; L
  25.         SetEvent(sleep->eventHandle);
    , W4 k1 M: a. ^" o5 q, G. {8 T
  26. ! K) e* @8 u# l4 ~$ X
  27.         return 0;
    - t" u- e! f5 d" w
  28. }- {( q: ^2 w2 B
  29. 4 P3 G, v3 r- S+ L
  30. //////////////////////////////////////////////////////////////////////
    8 j8 M, J0 M( i2 A) u
  31. // Function  : XSleep()* \! V  Q  c2 ]; X# d
  32. // Purpose   : To make the application sleep for the specified time
    2 X$ U9 e" y7 J% b
  33. //             duration.
    / U8 Q6 |# K! g1 @& y- i+ T0 y
  34. //             Duration the entire time duration XSleep sleeps, it
    0 [% q6 y5 ~# d4 F- k) q; b
  35. //             keeps processing the message pump, to ensure that all6 T6 }5 d, r$ A4 P1 o, o) N
  36. //             messages are posted and that the calling thread does0 u9 v% C0 f( ~
  37. //             not appear to block all threads!
    " c4 _1 I% A+ g$ ?4 b# {
  38. // Returns   : none( T  {4 C" O' E4 S) }/ b8 X' L
  39. // Parameters:      
    # t$ _, Y- v3 D3 Y* t" i
  40. //  1. nWaitInMSecs - Duration to sleep specified in miliseconds.6 k7 s) |8 U8 L6 l/ h
  41. //////////////////////////////////////////////////////////////////////
    4 n8 z& u  h9 ?& G& L* Q9 N# l3 V
  42. void XSleep(int nWaitInMSecs,int nAscll), B7 u/ |8 N9 K  K' ^$ [7 t% Q
  43. {2 }$ h: ^  ]8 O$ H! K5 ^
  44.                 9 c8 Y* \0 A7 w+ `; V, C: p& N
  45. <blockquote><span class="Apple-tab-span" style="white-space:pre">        </span>INPUT input[2];
复制代码

; n; _  i* b) z5 J( Z! K! E, L( \  w5 @9 @9 W4 ?6 H% E8 n+ \
xsleep.h文件代码
4 ~; {4 k2 ?- X; E6 B% I' P
: Z2 R5 s  H5 B( a) n2 W
  1. //Download by http://www.cncml.com: A1 y2 t4 h, }' f3 ]- o  _
  2. #ifndef _XSLEEP_H_
    3 X1 v' x& o' |! `& s1 z8 s
  3. #define _XSLEEP_H_
    3 \! X1 g8 o  L1 z9 J% P
  4. 9 D* X! t& J4 _0 l
  5. void XSleep(int nWaitInMSecs, int nAscll);
    7 |: l9 @" L1 o4 ?$ {5 e+ _0 r+ U8 ^7 k

  6. : }9 G, y7 \7 t5 p& R
  7. #endif // _XSLEEP_H_
    5 S# h  R, |$ m: \* A
复制代码
1 \/ Q5 o2 X) K0 E: C* y# [

* G" y6 t& H- f. @mfc中的调用代码如下
- A2 Y! Y- b$ Y, h4 U( ^2 j! S! c: u
  1. int ascll;
复制代码
  1. XSleep(500,ascll);
复制代码

3 o6 D4 O0 g9 G7 g3 ]! S
2 I! l' k. y# R/ O
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-6-18 20:59 , Processed in 0.111010 second(s), 19 queries .

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