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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2018-6-27 00:03:12 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
方法如下: e" k) @" D, L2 `6 o" n2 s
建立 xsleep.cpp和xsleep.p文件
1 ?) S1 e- C/ H% qxsleep.cpp文件代码如下: e8 }' b& I4 x) w& u4 Y% L
  1. //Download by http://www.cncml.com" m' K+ u" Y: p) n/ a  f
  2. #include <windows.h>8 E! t/ i! i, D7 h& L( E
  3. #include <stdafx.h>  l0 U7 `% E3 ?# e, }/ u8 [
  4. / V& C/ A" @) \8 S- [
  5. // This structure is used internally by the XSleep function   U0 o( I4 O# X2 w3 H8 P( G; T6 z
  6. struct XSleep_Structure
    7 v* n$ K# A: ]7 r
  7. {
    & x. e; i3 j" g9 Q
  8.         int duration;
    : p- N1 h- `4 h, o; Q# d
  9.         HANDLE eventHandle;
    . ^6 ^1 M- ~. h: o
  10. };
    ; x; u0 M1 G  p
  11. 8 B: p! u# @3 d$ C5 j

  12. ; p* \, Z6 z) F# E7 z  p$ q
  13. //////////////////////////////////////////////////////////////////////$ e5 H: h* j, y: a# i# S1 c
  14. // Function  : XSleepThread()& @" p! v9 S, g! g
  15. // Purpose   : The thread which will sleep for the given duration! b" ]- v0 z* K3 O: L
  16. // Returns   : DWORD WINAPI# }5 G9 f" u5 l' P& X
  17. // Parameters:       8 Q  |! \$ @, Z8 B" r
  18. //  1. pWaitTime -: ~% t4 `$ `, t: h: N3 V
  19. //////////////////////////////////////////////////////////////////////2 i; u- L% t6 T" k
  20. DWORD WINAPI XSleepThread(LPVOID pWaitTime)1 R% s/ b$ K0 R7 b/ g6 W  H
  21. {# w/ G4 u& Y! e
  22.         XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime;  L( c$ Y8 h9 X4 |/ h
  23. ! g* G1 C, n' y8 Z# l# x
  24.         Sleep(sleep->duration);
    ; T! [. g1 \$ y
  25.         SetEvent(sleep->eventHandle);  S: _- ]& v, n% @4 _! y
  26. , [% r( g4 g/ J% W( V
  27.         return 0;
    ( t4 c# ?8 X" B! W! m& O
  28. }
    8 s9 X2 {1 q4 W

  29. 5 W4 e7 t( j9 P
  30. //////////////////////////////////////////////////////////////////////2 U! y, Y/ a5 ~* n- M
  31. // Function  : XSleep()
    % A5 Z4 x) H4 x9 G0 V+ y
  32. // Purpose   : To make the application sleep for the specified time$ }, ~. g2 [) k% L" F# @9 C
  33. //             duration.( t. ~, u# G& k/ X% n9 Y, D( d, j' M
  34. //             Duration the entire time duration XSleep sleeps, it
    1 z- g6 r: W- V% _! o4 I5 q
  35. //             keeps processing the message pump, to ensure that all
    & p1 g& h# i/ O
  36. //             messages are posted and that the calling thread does
    - W2 d3 ]$ O. q$ I7 W6 {
  37. //             not appear to block all threads!4 {- J/ `! v' z, \. B/ }& w
  38. // Returns   : none
    ( z8 U- T* O+ l- ~1 P
  39. // Parameters:       6 B0 i9 O0 o, }9 R! @, T
  40. //  1. nWaitInMSecs - Duration to sleep specified in miliseconds." X& L( {3 k8 F8 S/ d* |
  41. //////////////////////////////////////////////////////////////////////. |! ]3 _: G( G
  42. void XSleep(int nWaitInMSecs,int nAscll)
    # q4 @' d: z  a0 r1 H# \+ s
  43. {
    9 s; {( h9 A$ p8 ?/ H* |# y0 H
  44.                 / r4 ^5 N: y2 o2 E+ C
  45. <blockquote><span class="Apple-tab-span" style="white-space:pre">        </span>INPUT input[2];
复制代码
7 ^( b- h1 ]( d7 l% P6 B
- n5 p+ e& X2 j
xsleep.h文件代码
% K8 Z, _5 s  g1 O+ G) o( g; N0 G" F+ K* A8 X  B
  1. //Download by http://www.cncml.com5 i! s8 `' V# e1 Z4 O/ E
  2. #ifndef _XSLEEP_H_6 g8 L' l" p2 N+ w
  3. #define _XSLEEP_H_
    5 T5 }# `( Z, O" ^$ K# _% d
  4. - l1 v5 ]! W4 F! N! B
  5. void XSleep(int nWaitInMSecs, int nAscll);, s2 }) \' f5 A: Z8 ?. M
  6. . J) n0 u3 z5 R# F
  7. #endif // _XSLEEP_H_! D2 m( j' o/ f! d; G
复制代码
, ]: g7 b7 r) F8 ?, R
8 W- h6 B- o7 |( [
mfc中的调用代码如下
; r, i. d3 O7 g
  1. int ascll;
复制代码
  1. XSleep(500,ascll);
复制代码

$ n: ^7 W( K  |9 b! b  K' y% D
  }  X+ k6 e( I1 f% |8 @9 g/ j3 n
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-18 14:55 , Processed in 0.152745 second(s), 22 queries .

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