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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-6-27 00:03:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
方法如下  y: l; L: i5 W2 d9 ?
建立 xsleep.cpp和xsleep.p文件
4 {' X* x$ K8 H: f, n( ?xsleep.cpp文件代码如下8 X8 E: R$ |7 E: R( o
  1. //Download by http://www.cncml.com& M' \  p* Z6 g
  2. #include <windows.h>; e$ Z  T# j! a9 |7 I
  3. #include <stdafx.h>
    - V. d7 d& N9 y

  4. . x: e1 P" n6 N2 i. E4 n
  5. // This structure is used internally by the XSleep function
      @5 j& }8 c9 a. ~" Y) ]5 }, v
  6. struct XSleep_Structure5 u9 Q7 ]' s$ P4 i4 M
  7. {
    % A* x1 z9 D. l& @
  8.         int duration;( a& Q1 s7 H! s- i! F4 P
  9.         HANDLE eventHandle;
    $ n7 J7 {$ ^; w: A# G" k
  10. };6 m) J: X1 s- P/ _

  11. ) D3 S1 [" I* m. \7 X# O

  12. 8 @1 m3 O  O" c% K$ e
  13. //////////////////////////////////////////////////////////////////////& o% [+ l5 G$ Q2 C
  14. // Function  : XSleepThread()
    7 W- O: \) e) `
  15. // Purpose   : The thread which will sleep for the given duration! K& Y  N8 }  R. Y7 C9 V
  16. // Returns   : DWORD WINAPI
    " F  X' i9 r2 M7 M' d
  17. // Parameters:       7 Y/ I3 a  T) I8 {
  18. //  1. pWaitTime -
    , b) {2 p7 t" {+ C& O5 K0 @
  19. //////////////////////////////////////////////////////////////////////* c% q* g/ L5 D9 Q, R# V6 x  c
  20. DWORD WINAPI XSleepThread(LPVOID pWaitTime)
    0 I8 N& R) k% n
  21. {* g$ C$ i* h) f  F
  22.         XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime;
    6 h4 L( @% c" a! n' f3 W
  23. 8 T5 X0 e( [0 y
  24.         Sleep(sleep->duration);
    & ^1 n3 }: N' h& |
  25.         SetEvent(sleep->eventHandle);
    / Z; l( @9 K4 B

  26.   r5 z5 j7 f2 R. G& N" k* V
  27.         return 0;
    9 b1 v+ F- b2 J: \' a
  28. }
    # n$ g" p  e3 Z1 c9 Q+ v6 R: Y

  29. ; d; T! E9 O. P2 B
  30. //////////////////////////////////////////////////////////////////////
    ; u. f  K/ w; Q2 b% p* x  e2 {
  31. // Function  : XSleep()
    / b! ~0 ^$ R! [/ s
  32. // Purpose   : To make the application sleep for the specified time
    6 R5 [) ^# A% E8 H  _8 W
  33. //             duration.
      n' Y7 f: Z% E9 P7 u" J
  34. //             Duration the entire time duration XSleep sleeps, it
    5 [* D) B3 \- ]5 x3 d- r
  35. //             keeps processing the message pump, to ensure that all! M, P+ x+ H4 D9 l1 R- q* R0 c! A
  36. //             messages are posted and that the calling thread does
    " @! Z4 }& w) E+ \, b7 B+ L
  37. //             not appear to block all threads!
    # r4 O2 p% O" N2 K' k
  38. // Returns   : none! p. ]: g0 k0 O" J5 Y; \! Z; |, [
  39. // Parameters:       0 D' g7 I# F6 O- \1 {
  40. //  1. nWaitInMSecs - Duration to sleep specified in miliseconds.
    ! w5 R5 `  f8 l, L, ]7 b% {+ @
  41. //////////////////////////////////////////////////////////////////////
      f5 p/ A+ @' \! B" J! f
  42. void XSleep(int nWaitInMSecs,int nAscll)
    : P9 f0 h  M1 v- s4 m
  43. {
    ) [5 v* b. @- c2 D2 u  U
  44.                 5 I; y8 k# w  v, R. X
  45. <blockquote><span class="Apple-tab-span" style="white-space:pre">        </span>INPUT input[2];
复制代码

% \* F6 F7 X: N9 y7 h4 r8 Q) M5 w+ U
xsleep.h文件代码
/ A# B* |; w: v; Z* }( M6 @: l& M! K) @
  1. //Download by http://www.cncml.com
    : d- J2 s$ ]) ?( x# `
  2. #ifndef _XSLEEP_H_3 l4 |. `$ q5 O
  3. #define _XSLEEP_H_8 J7 l( r+ ]0 R: I, a4 A

  4. 4 x/ }& Q2 P% N/ C. m2 X
  5. void XSleep(int nWaitInMSecs, int nAscll);1 U, s7 ~  n2 o0 b/ b+ v4 A

  6. 2 e) x4 r8 O* Q: w  {) M0 S! k! v
  7. #endif // _XSLEEP_H_
    1 p3 ?- b" X# \
复制代码
6 {* W: e: W# C2 `9 K, V
* i6 P! L7 H+ A$ L) C+ X: d" @
mfc中的调用代码如下
$ E. D# r* L: J. i4 J7 Y2 Z( ]
  1. int ascll;
复制代码
  1. XSleep(500,ascll);
复制代码

/ g" Z0 g8 R7 H1 L4 T2 I4 b: {7 q& \. ^. h3 f( z1 X7 ]
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 15:55 , Processed in 0.044962 second(s), 20 queries .

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