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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-6-27 00:03:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
方法如下$ y4 r9 Q* [/ z7 s% m8 |% @( I) i
建立 xsleep.cpp和xsleep.p文件. I# d( M0 G1 T, n% [3 n9 S& h5 V: G
xsleep.cpp文件代码如下
2 y9 G) |0 [3 y; N9 J
  1. //Download by http://www.cncml.com3 ^! U( b5 k% o9 W( q% f- G& }9 d
  2. #include <windows.h>
    ! I* a  }8 _) p4 s4 K
  3. #include <stdafx.h># ]2 C- R& h+ N

  4. 4 i+ Z2 i" }2 {- S0 |7 E
  5. // This structure is used internally by the XSleep function - a2 u; b7 l, y3 C! M& {
  6. struct XSleep_Structure2 L! v( F- M: ~( ?
  7. {
    & d3 V8 c! H4 H  j/ i
  8.         int duration;
    : ]1 y! z7 V$ R' J8 {7 J) Y
  9.         HANDLE eventHandle;# S" n& b, a' w7 n
  10. };
    : A  `. r  k6 ~  x- |. Z% D

  11. 1 w: w: t. \, e* S0 ]. u
  12. . ^. m, i9 |- }: a2 A+ B
  13. //////////////////////////////////////////////////////////////////////# r9 I! P( P. v1 t
  14. // Function  : XSleepThread()
    7 s( d) x, \+ N1 {6 J1 Z  @
  15. // Purpose   : The thread which will sleep for the given duration
    2 M1 o$ c( @& h  ?) ^
  16. // Returns   : DWORD WINAPI; s+ y8 O6 ~3 W1 }+ O. e6 H
  17. // Parameters:       , W$ }' N, w) r& `, [- v: C* e
  18. //  1. pWaitTime -( E) b" B, i6 Y( \* y3 P
  19. //////////////////////////////////////////////////////////////////////
    3 x$ k4 H' Z5 M$ T( X# v9 |
  20. DWORD WINAPI XSleepThread(LPVOID pWaitTime)+ k4 F9 S+ y! Y  Z" I& l0 S
  21. {4 q0 g4 t# y$ y& h
  22.         XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime;
    2 f. C+ S* B- _$ D
  23. # ?0 q% u+ \$ p; A
  24.         Sleep(sleep->duration);* d; q! l1 \3 d8 T* W5 `# l* M
  25.         SetEvent(sleep->eventHandle);
    : d& D3 G) u5 k2 i& n2 N
  26. ; Q4 V% B( r0 @) O" d* p
  27.         return 0;3 V* u8 {# t* P
  28. }0 ^+ a# ?7 g) k
  29. 6 a! R  Z, P  N% u% p, p/ b
  30. //////////////////////////////////////////////////////////////////////+ P- E9 k0 R1 L2 y
  31. // Function  : XSleep()$ S* V1 a% e3 e. ^) a, ~
  32. // Purpose   : To make the application sleep for the specified time
    + y) J+ d0 O. ?+ V
  33. //             duration./ a6 `% Z! H8 H! Y$ I, H/ ~
  34. //             Duration the entire time duration XSleep sleeps, it
    0 L# Q2 M! k8 j3 q3 u
  35. //             keeps processing the message pump, to ensure that all5 E( m: l, ]3 o0 y" u, P0 d
  36. //             messages are posted and that the calling thread does
    $ F5 N$ b. H' R! {% J
  37. //             not appear to block all threads!/ D  l! A7 x0 l
  38. // Returns   : none9 i9 e0 {- f. W; ~" s% f) Q/ K
  39. // Parameters:       0 \$ x9 I8 w' ?6 c
  40. //  1. nWaitInMSecs - Duration to sleep specified in miliseconds.5 j# A- w9 b3 t( U! r9 }
  41. //////////////////////////////////////////////////////////////////////
    ) r2 t4 x) r2 H8 E# C7 T
  42. void XSleep(int nWaitInMSecs,int nAscll)$ z+ i+ a) `5 s- {' J1 E; Y
  43. {
    1 M# u' D0 O' u
  44.                 7 m$ n# a2 O4 \0 w
  45. <blockquote><span class="Apple-tab-span" style="white-space:pre">        </span>INPUT input[2];
复制代码
; l4 a6 O6 Z4 m- \0 k% D5 \; c; ~

; L4 ~3 H- F/ }xsleep.h文件代码( W5 K/ m6 n- [- X% q$ x1 \

  c; i0 n" {1 p7 z5 [4 o" E
  1. //Download by http://www.cncml.com
    ! u( f- o: \8 w2 s+ C
  2. #ifndef _XSLEEP_H_
    6 h7 i. {+ d$ o0 T; n
  3. #define _XSLEEP_H_9 F# C0 D" F" O0 l- d

  4. $ f2 o& L) e  Z1 Z- t1 q# b
  5. void XSleep(int nWaitInMSecs, int nAscll);+ s0 M7 |6 A( ?4 M8 @) u# s$ m
  6. % m/ S% S( C$ g0 z! h
  7. #endif // _XSLEEP_H_
    , ]5 m" d3 B' g
复制代码

+ M( N$ n/ p- b, k2 W0 }9 R* w/ i/ I2 r( ^
mfc中的调用代码如下
4 Y" S) [" u8 D7 N+ a
  1. int ascll;
复制代码
  1. XSleep(500,ascll);
复制代码

  Y- K3 Y' B) U5 b' U! u; ?7 t# |4 t; {1 C! \5 `5 B: m
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-17 23:27 , Processed in 0.078506 second(s), 19 queries .

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