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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-6-27 00:03:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
方法如下. g; \! R) E- g0 p& Q
建立 xsleep.cpp和xsleep.p文件  @6 V% P7 M, o$ Q3 E% V1 K
xsleep.cpp文件代码如下
+ w1 l( H1 u/ f; v, i) i: L
  1. //Download by http://www.cncml.com
    , M+ t: a* n0 K) n1 e6 r& z! R2 \
  2. #include <windows.h>
    ) _* l- ~" T: t1 u/ Q+ ]  }# c
  3. #include <stdafx.h>3 Q+ x  o$ D1 B) I

  4. ' ^& p  Q$ {! D
  5. // This structure is used internally by the XSleep function . F2 h( j9 |) O4 m3 a
  6. struct XSleep_Structure
    : t6 i0 D5 b. [* p
  7. {& Z! Z, M4 Z% }4 {1 c* [
  8.         int duration;. a( K1 s: g; i- I
  9.         HANDLE eventHandle;2 V1 X; L0 f- [7 V& W
  10. };% X2 t$ I$ Q" _6 ]) p' z% @
  11. - p6 G8 U" R7 [; @& n  _
  12. * L2 X  a8 z$ g) `% ]% ^
  13. //////////////////////////////////////////////////////////////////////
    8 m0 X) E/ s1 ]8 C; h8 V, t
  14. // Function  : XSleepThread()& f3 }4 f& `3 W  u/ J3 \
  15. // Purpose   : The thread which will sleep for the given duration
    " r2 t; X. h9 D$ J/ {
  16. // Returns   : DWORD WINAPI5 S; H- c) P& X% j4 N
  17. // Parameters:      
    + U2 u6 ?8 W6 W5 z& V: g
  18. //  1. pWaitTime -
    2 h4 y; b/ H+ e& f0 G" R* G
  19. //////////////////////////////////////////////////////////////////////
    . s* \; ?5 W" x9 E: I
  20. DWORD WINAPI XSleepThread(LPVOID pWaitTime)
    / Z' f+ W1 v6 |* q: F6 g# Z
  21. {
    # p4 [) G% P* @5 o. v
  22.         XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime;
    4 w7 ]  G, E8 m; b: I
  23. 8 s& l2 U- S( j/ y6 O4 @0 ?
  24.         Sleep(sleep->duration);
    4 o8 t5 l8 l- |5 {
  25.         SetEvent(sleep->eventHandle);
    + P" H8 _' N1 N9 E( G

  26. & g* x# J( t8 B! g8 i
  27.         return 0;/ n6 m; V4 @' e, H3 R3 C; M
  28. }, O/ `' S2 W  k) x

  29. # W( c' B# C" c) Y, Q# J. i# X; P
  30. //////////////////////////////////////////////////////////////////////
    ! U* A( X4 m- l3 M, S
  31. // Function  : XSleep()
    6 k1 G; S$ |  z
  32. // Purpose   : To make the application sleep for the specified time
    : N' P/ l1 M" t* [/ |4 ?1 m
  33. //             duration.# h4 T, b2 L: |& l3 W
  34. //             Duration the entire time duration XSleep sleeps, it2 S, G# x- L2 a. p' V
  35. //             keeps processing the message pump, to ensure that all
    3 q* w2 R( w' g* r8 V3 q- T
  36. //             messages are posted and that the calling thread does
    5 K, P7 b5 O& k' P3 K; I
  37. //             not appear to block all threads!
    + @) V$ d8 k) M) Q  |" H9 P, X
  38. // Returns   : none
    4 D1 h' U5 V/ f4 K
  39. // Parameters:      
    9 d4 f1 m- L. X( r7 A5 s1 u
  40. //  1. nWaitInMSecs - Duration to sleep specified in miliseconds.
    6 s1 m" y2 ~/ l/ W/ Q, K, H! y( S
  41. //////////////////////////////////////////////////////////////////////+ k* R1 x: g) V# x' u
  42. void XSleep(int nWaitInMSecs,int nAscll)
    ) L* M' v, E. ?) H- W9 f5 Q* o1 h
  43. {
    $ m) F4 t8 z! c& j
  44.                
    4 M0 P2 @- o/ D! u+ E; w* S
  45. <blockquote><span class="Apple-tab-span" style="white-space:pre">        </span>INPUT input[2];
复制代码

1 N. t& S0 b8 P+ c4 f6 ~0 a# T# o4 \# G& f/ c
xsleep.h文件代码
- {6 e  R9 V: y" F& }& G7 Q
6 T/ G( Y5 z# j: G
  1. //Download by http://www.cncml.com3 w0 R6 B  L# h1 a2 k( p( r
  2. #ifndef _XSLEEP_H_
    - j  Z1 r5 D; f. ?2 T
  3. #define _XSLEEP_H_
    9 ~4 B. e7 ~$ M2 S* y8 Q

  4. 2 U2 L& i% G3 H4 {2 ?/ f
  5. void XSleep(int nWaitInMSecs, int nAscll);0 M  G0 E# J* v' ?
  6. ( P0 I, F& e. ~) L$ ?: ~- N
  7. #endif // _XSLEEP_H_! k$ N3 R/ A" d
复制代码
/ P4 q/ _( P$ ]1 _+ p% ]

: ?' g8 J: [- ?0 Kmfc中的调用代码如下
. B4 n; F) G- B* Y5 m0 u
  1. int ascll;
复制代码
  1. XSleep(500,ascll);
复制代码

( g: j& g4 |# U/ o6 e2 l
1 w4 J% G4 P2 w0 z: ^0 P2 U
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 14:55 , Processed in 0.050118 second(s), 19 queries .

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