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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-6-27 00:03:12 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
方法如下
, h) H7 h" K7 Y% f4 L9 C1 L建立 xsleep.cpp和xsleep.p文件
2 t1 G/ |+ g' D4 dxsleep.cpp文件代码如下% F; n4 Z) o2 r
  1. //Download by http://www.cncml.com3 P/ l+ K/ v: q' Q4 ^" s8 q* W( _
  2. #include <windows.h>: h) G! ]" }; P1 l1 ]; `
  3. #include <stdafx.h>
    . b9 Q7 Q4 {. W6 C# V

  4. ! N+ H9 \7 N$ {
  5. // This structure is used internally by the XSleep function 6 g. f- H) ^: Y
  6. struct XSleep_Structure, y# G6 w; @. y) `7 G
  7. {
    5 J9 r0 Y* F" V) R
  8.         int duration;# n4 Z. }. n4 X+ H& v# o1 @& X
  9.         HANDLE eventHandle;3 k6 L$ x3 J" ~. U1 c7 b
  10. };& B$ j0 u( [1 N! h( Q& o

  11. - q5 A: ]) j% e/ u' q

  12. $ n  i0 {" p! D2 ^' _6 C0 z: T
  13. //////////////////////////////////////////////////////////////////////
    6 K& X3 F5 j: k3 ^
  14. // Function  : XSleepThread()- v  ?% ?- [" g0 K! Y
  15. // Purpose   : The thread which will sleep for the given duration! [* F2 ?. f: h& G' \* G, S$ [; s; v
  16. // Returns   : DWORD WINAPI
    / @6 k5 ?9 u/ U! h
  17. // Parameters:      
    . F" L* v$ K4 ]( m$ A' t* d
  18. //  1. pWaitTime -
    . {9 W1 W) s& I
  19. //////////////////////////////////////////////////////////////////////
    6 J* ]/ V. I8 m, B5 `8 C
  20. DWORD WINAPI XSleepThread(LPVOID pWaitTime)
    1 N, R, S1 t( G- K, P8 |; X  {# A  u
  21. {3 r0 u' P& ?* v+ I' l5 _
  22.         XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime;
    3 I3 x. }* e* o
  23. & ^( w$ _9 u0 h/ ^
  24.         Sleep(sleep->duration);9 p/ ], Y- b+ l! Z5 E; ^, t
  25.         SetEvent(sleep->eventHandle);
    / c5 G: G, Z/ M# K% Y6 Y
  26. & _4 x' d* o8 I( G- ]/ G* o
  27.         return 0;
    # P1 @4 C1 }: r
  28. }7 {: C2 \  R' f+ i1 f
  29. 6 B; w) n6 T) [  _4 v1 f8 E# c, b
  30. //////////////////////////////////////////////////////////////////////6 w$ z- m+ y/ d5 ^6 Y+ P
  31. // Function  : XSleep(), H$ n4 n( ]; [) @. p$ D
  32. // Purpose   : To make the application sleep for the specified time
    2 W8 Q3 q- R. m3 u) `' E
  33. //             duration.
    6 H4 ?: o7 P) Q" V, X
  34. //             Duration the entire time duration XSleep sleeps, it" C/ [4 E2 |( t
  35. //             keeps processing the message pump, to ensure that all
    ! _6 x. c" o& B" q0 b
  36. //             messages are posted and that the calling thread does2 X9 M, z1 j: B  Z1 @; h) e$ \
  37. //             not appear to block all threads!
    ( [- n, i* ]6 h) c, T
  38. // Returns   : none
    ' D# U  I  f( O$ _' f
  39. // Parameters:      
    3 U/ H% v% _$ G# C8 c) n* \
  40. //  1. nWaitInMSecs - Duration to sleep specified in miliseconds.$ X2 h& J, o+ d% u1 |! C
  41. //////////////////////////////////////////////////////////////////////
    2 o" [: z. V% Z% J
  42. void XSleep(int nWaitInMSecs,int nAscll)
    . r( K# f- F! y5 Z: k
  43. {2 r$ K5 j% X8 n! i* ^0 T
  44.                 7 z2 |3 E; h( y  O
  45. <blockquote><span class="Apple-tab-span" style="white-space:pre">        </span>INPUT input[2];
复制代码
: a, K  E6 ^  D% G, \# j
5 K* v5 G, P1 l# I# r
xsleep.h文件代码6 e" X. X! i' h
9 c, R/ Q. b2 V! f' Z6 T7 B
  1. //Download by http://www.cncml.com6 o) o0 C0 O- a5 {3 E/ M
  2. #ifndef _XSLEEP_H_8 e4 R1 G+ c( i; O% T
  3. #define _XSLEEP_H_$ o4 r/ w$ w  x( L9 y4 X$ G

  4. 0 s. i/ b' m: z
  5. void XSleep(int nWaitInMSecs, int nAscll);! c( O3 P, w& h5 `' R0 |1 G" m
  6. ; w4 w, s( {+ ?) O
  7. #endif // _XSLEEP_H_% V" z: _) K9 q/ l
复制代码

4 Q* U6 k/ r( a& e% k4 `
" q: O+ {5 O) A. l* Q$ kmfc中的调用代码如下
$ r9 ]) t8 m3 E. s+ t0 [$ D
  1. int ascll;
复制代码
  1. XSleep(500,ascll);
复制代码

: }0 B8 l& l- t. L/ i! x4 P0 R6 L  t  y
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 16:01 , Processed in 0.061177 second(s), 20 queries .

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