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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-6-27 00:03:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
方法如下1 d  T* N( o: ~, C- s7 V# ^) M+ E( w1 n
建立 xsleep.cpp和xsleep.p文件
% Q6 J! R; P. j, P8 ?+ `1 u; H# Z2 [% ~xsleep.cpp文件代码如下
7 J1 }2 D, @2 I
  1. //Download by http://www.cncml.com
    4 z  A9 S$ T# w! f7 o  [. b* M
  2. #include <windows.h>+ x8 W  x1 B7 J2 S- x
  3. #include <stdafx.h>
    ' T# C( ?9 c0 ?5 h

  4. 4 V9 L- h9 M5 Z3 D" b7 X
  5. // This structure is used internally by the XSleep function . R$ G9 f3 @  ^& ~* Q2 e$ n
  6. struct XSleep_Structure
    9 {2 E5 E$ c( D- {( {
  7. {
    . i; W8 Y1 A& }2 a4 c9 M6 u
  8.         int duration;& w5 \7 c; }: ~2 g
  9.         HANDLE eventHandle;% V! u# R0 t+ ~2 [( a, r
  10. };
    - _4 W( u$ J1 q+ C1 ]
  11. 1 }0 {+ |3 m9 ^0 u$ {) b
  12. 5 \7 h: `( W" _* s
  13. //////////////////////////////////////////////////////////////////////
    ! y/ Y# B% Z0 E( i  T& D* d
  14. // Function  : XSleepThread()) r+ @8 ^. M6 \( h6 {9 Y0 i
  15. // Purpose   : The thread which will sleep for the given duration. f. p0 l$ |9 L8 H/ a1 s* z  p/ b
  16. // Returns   : DWORD WINAPI
    " Q9 B( F5 i& j. \$ S+ U+ G
  17. // Parameters:      
    ' a' k. {7 B( y- q' q' k4 k
  18. //  1. pWaitTime -
    * e3 F& `4 k9 @0 K" A5 g
  19. //////////////////////////////////////////////////////////////////////  ?/ y+ U9 {5 H
  20. DWORD WINAPI XSleepThread(LPVOID pWaitTime)
    - E  g# e2 [; e8 o* i2 J. v1 f
  21. {
    . d; v/ D5 t* ]6 }' C3 B
  22.         XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime;
    - b' [2 B4 U- p+ X0 ~( E* Q
  23. 7 F) e* W$ E, j) I, F7 y4 x. B. y
  24.         Sleep(sleep->duration);8 S$ i" a  j+ e! K* @6 p
  25.         SetEvent(sleep->eventHandle);# M3 {% H7 l$ r9 k% ^$ c* F2 X

  26.   L! H; h  Z" H# F7 e  {
  27.         return 0;
    ! S, t7 b1 r. w5 l
  28. }2 m1 b/ J) m" s  V

  29. , R' p# ?0 S8 V% }0 g, s  I, \" {, ]. @0 G
  30. //////////////////////////////////////////////////////////////////////
    3 ?' K$ [- B- f
  31. // Function  : XSleep()9 o* K8 G! {; v9 n
  32. // Purpose   : To make the application sleep for the specified time
    : p; b8 V8 C, f2 c0 Q
  33. //             duration.
    2 n/ S4 i" V8 u3 r" T5 C
  34. //             Duration the entire time duration XSleep sleeps, it. E* `: J/ s: z& L2 _' N
  35. //             keeps processing the message pump, to ensure that all! t( G3 j: G4 L2 x! U+ H
  36. //             messages are posted and that the calling thread does
    7 ^2 _7 V# f" H4 l# J' h' i7 F
  37. //             not appear to block all threads!
    6 `* @  l2 k5 U4 O+ c2 h
  38. // Returns   : none
    7 C' E1 \+ T/ w+ P1 y
  39. // Parameters:      
    # \' J  R/ `( ^. k
  40. //  1. nWaitInMSecs - Duration to sleep specified in miliseconds.
      }! q1 g( {8 T* {
  41. //////////////////////////////////////////////////////////////////////
    0 E6 Q1 W* s) S
  42. void XSleep(int nWaitInMSecs,int nAscll). v8 J0 V3 |! Z
  43. {
    9 C, L" o( e0 C
  44.                 $ ^' N( {5 H  W% G1 u( U! K8 l* @: H
  45. <blockquote><span class="Apple-tab-span" style="white-space:pre">        </span>INPUT input[2];
复制代码

* R+ g3 c, S( R! f& g4 _& a) d; l* }- P( r0 l
xsleep.h文件代码
9 E& E9 |% o  b: _( T2 r# X
$ X8 o2 n: Y2 E- q8 e
  1. //Download by http://www.cncml.com
    ' @3 r$ M0 H! U
  2. #ifndef _XSLEEP_H_
      ^4 d3 l: S& o3 T% Y% w* |! w* K
  3. #define _XSLEEP_H_. I- p) H+ [+ X0 B

  4. 5 R6 r- k/ m) J+ a0 V
  5. void XSleep(int nWaitInMSecs, int nAscll);. K4 b: D2 s6 V( U$ J0 C  L7 O) F2 q

  6. $ v3 ?' A! K& `  N  g
  7. #endif // _XSLEEP_H_5 N0 P, @# a; \  G! P
复制代码

5 ~0 d7 ?: E* b( I3 X' U( U) d) Z' T" ], L* }/ H1 d& O
mfc中的调用代码如下
# b$ A( Q7 |9 U9 d
  1. int ascll;
复制代码
  1. XSleep(500,ascll);
复制代码
1 p& R& c! F: L7 W/ c- l8 I
9 s* k* L( n$ u. `# M  f
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-9-22 04:12 , Processed in 0.050767 second(s), 19 queries .

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