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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-6-27 00:03:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
方法如下/ V. G1 [" H( X) e0 b# |
建立 xsleep.cpp和xsleep.p文件
; M9 T1 I& G3 f/ a" \( [. yxsleep.cpp文件代码如下4 |3 p2 r' N7 ^; D8 K. A- q% ]
  1. //Download by http://www.cncml.com% l( C# j1 `9 k2 A; L. q
  2. #include <windows.h>& n# d' ?' |4 l. D3 F
  3. #include <stdafx.h>
    * L: _+ x) d# t' P; d! p5 x5 w9 i# Q! W1 S! G

  4. + M& Y$ f  _& Z4 A% D% W  e
  5. // This structure is used internally by the XSleep function
    7 I9 d$ U% a9 _0 `: U& C! Z+ _
  6. struct XSleep_Structure
    3 D8 g% t% ?5 e2 H3 ~7 Y" s+ o% R
  7. {
    1 b- G, i4 D; p# t, @
  8.         int duration;. I6 K; w# I4 M- Q8 z4 A0 g
  9.         HANDLE eventHandle;; z* V; j: K/ R2 D/ Q, O1 l+ \" [
  10. };( J. V3 }7 B( u6 s* p
  11. ' c+ `4 q" \( Y$ ]5 S$ C3 M
  12. / i! v2 p" O* k% k0 j! F7 [
  13. //////////////////////////////////////////////////////////////////////
    " _/ m) q0 n  X2 h! y! f# {
  14. // Function  : XSleepThread()
    : `5 n% s* O  v# l! h  C
  15. // Purpose   : The thread which will sleep for the given duration
    , r% b" d$ ~7 V2 G: O
  16. // Returns   : DWORD WINAPI0 [! g( Q# g; q  X
  17. // Parameters:       ; ]; I- H* @: h- e3 X. n% i
  18. //  1. pWaitTime -
    # Z8 G6 A3 k( \0 r1 m
  19. //////////////////////////////////////////////////////////////////////: M& `4 m% ]- @6 ^: Z
  20. DWORD WINAPI XSleepThread(LPVOID pWaitTime)+ A& l* V/ |3 N+ C0 G) M$ h
  21. {
    * k4 A6 ?" R9 T2 I
  22.         XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime;
    5 H, R5 ^2 e) t8 [  v1 V! X( c

  23. " H6 L1 O! T9 L3 S3 q2 h
  24.         Sleep(sleep->duration);" K/ u& q0 y3 X0 H: c3 y
  25.         SetEvent(sleep->eventHandle);1 q; V" P' b, V3 z6 }  G+ v
  26. 9 c; O; ?# z6 A% d/ }2 l5 G
  27.         return 0;
    3 N6 C# E, f2 n+ S0 t
  28. }" k' x1 Y' j) e/ `- W) o
  29. 2 s+ f2 ]3 o1 A$ L: Z: ?
  30. //////////////////////////////////////////////////////////////////////6 T. `5 j$ v' @' m- [' x1 M$ d. [
  31. // Function  : XSleep()7 S/ p2 y% F) K, K. k' ]5 R
  32. // Purpose   : To make the application sleep for the specified time3 h+ G' L9 s6 L7 D* e# g) _7 g' x
  33. //             duration.
    , O% s9 G. N4 P1 j0 {$ l5 x
  34. //             Duration the entire time duration XSleep sleeps, it
    # z3 n: S, |, _# b
  35. //             keeps processing the message pump, to ensure that all1 ?, S4 _9 c7 X% b- y2 S
  36. //             messages are posted and that the calling thread does
    3 r6 r: S( n$ H
  37. //             not appear to block all threads!
    + R* [/ e" t  j8 q. m$ ~# d( o
  38. // Returns   : none
    ) N- \8 l, b1 d
  39. // Parameters:       " R: h/ s2 ^+ i5 m* P3 a  s5 p
  40. //  1. nWaitInMSecs - Duration to sleep specified in miliseconds.
    9 s) T8 w" Y$ T- S
  41. //////////////////////////////////////////////////////////////////////1 f5 x  a' X% {5 _5 f5 D
  42. void XSleep(int nWaitInMSecs,int nAscll)( G6 f0 B4 I/ F: e: Z4 U
  43. {6 ]9 m% ?+ m- M  M, m2 ]
  44.                
    1 S4 |. j6 F8 u/ y' Y
  45. <blockquote><span class="Apple-tab-span" style="white-space:pre">        </span>INPUT input[2];
复制代码
3 Z( j- s5 o* d# N5 l
" {' ?# h5 Y( G
xsleep.h文件代码4 n! k$ K/ |. {/ ^4 U9 w. s
1 Z5 L, \6 U9 `7 r# b6 C$ H1 U
  1. //Download by http://www.cncml.com
    % m! S& h0 X/ O& M" g5 Z6 F
  2. #ifndef _XSLEEP_H_
    ( R# @. f+ g  z  B; Q
  3. #define _XSLEEP_H_2 C+ Q4 i5 h3 G/ B3 N. `3 V

  4. * T; }+ l  q/ H! l% I) G
  5. void XSleep(int nWaitInMSecs, int nAscll);  u# c: k0 [8 l- f$ H* g. Q

  6. + E9 K' S) I# m
  7. #endif // _XSLEEP_H_, G' |/ o" H' M* L, m7 U1 f
复制代码

6 M# L, V. g5 E; s# H( m: @
; m* F& S- h. e& z. Cmfc中的调用代码如下& l% C) m9 V6 D9 {. s. w2 I
  1. int ascll;
复制代码
  1. XSleep(500,ascll);
复制代码
, L9 c* v0 {$ P5 W% A

! ~7 D2 s7 P( B7 S' R0 T2 t
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 17:24 , Processed in 0.078761 second(s), 20 queries .

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