T-Kernel 里 sleep API

本文介绍了T-Kernel中的tk_slp_tsk()函数用于指定毫秒级别的睡眠时间,并解释了当设置为-1时的任务唤醒机制。同时,还提到了Microwait系列函数如WaitUsec()和WaitNsec(),它们用于实现不精确的忙等待。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

T-Kernel 里有类似于 msleep() 的函数。

tk_slp_tsk ( TMO tmout ) ;

• TMO: Timeout time Time base 1 millisecond, 32-bit or higher signed integer (INT)

typedef INT TMO;

这里tk_slp_tsk可以指定tmout的值,表示睡眠的时间,单位是毫秒。

如果指定-1的话,必须有其他任务来唤醒该睡眠任务。


另外,下面函数也可以等待,但是是忙等。

参考:

http://www.t-engine.org/wp-content/themes/wp.vicuna/pdf/specifications/en_US/TEF020-S001-01.00.01_en.pdf

5.5.2    Micro wait
 - void WaitUsec( UINT usec )
    - void WaitNsec( UINT nsec )
        - usec   Wait time (microseconds)
        - nsec   Wait time (nanoseconds)
      Performs a micro wait for the specified interval.
      These waits occur in an ordinary busy loop, and as such are easily influenced by the runtime
      environment, such as execution in RAM, execution in ROM, memory cache on or off, etc. These
      wait times are therefore not very accurate.
      These waits are not the same as an OS WAIT state. The system state remains as RUN state.

相关的头文件参考:

tk/syslib.h

参考:http://monoist.atmarkit.co.jp/mn/articles/0608/08/news102_3.html



<think>我们正在讨论ExcelVBA中的延迟功能。根据引用[2]和引用[4],我们可以使用Sleep函数来实现延迟。Sleep函数是WindowsAPI的一部分,因此我们需要在VBA中声明它。步骤:1.在VBA编辑器中,我们需要声明WindowsAPI函数Sleep。2.声明后,我们就可以在VBA代码中调用Sleep函数,参数为毫秒数。具体实现:在模块的顶部声明Sleep函数:```vbaDeclareSub SleepLib "kernel32" (ByValdwMilliseconds AsLong)```然后,在需要延迟的地方调用:```vbaSub TestSleep()Debug.Print"开始"Sleep2000'延迟2秒Debug.Print "结束"End Sub```但是,引用[2]中提到,如果使用Sleep函数,程序会完全停止响应(即“卡住”),因为Sleep会挂起整个线程。为了避免界面无响应,我们可以结合使用DoEvents函数,让程序在休眠期间处理其他事件(如用户操作)。例如:引用[2]中给出了一个结合Sleep和DoEvents的例子:```vbaFunction delay_t2(t)Sleep(t)DoEventsEndFunction```但注意,这个函数在Sleep期间仍然会阻塞,直到Sleep结束才执行DoEvents。所以更好的方法是使用循环和DoEvents,如引用[2]中提到的另一种方法(使用timeGetTime): ```vbaPrivateDeclareFunction timeGetTimeLib "winmm.dll"() AsLongSub delay1(tAs Long)Dim time1 AsLongtime1 =timeGetTimeDoDoEvents'允许处理其他事件LoopWhile timeGetTime- time1 <tEnd Sub```这种方法在等待期间会不断处理事件,因此不会完全冻结界面。总结:1.如果只是简单的延迟且不介意界面暂时无响应,可以使用Sleep。2.如果需要保持界面响应,建议使用timeGetTime循环配合DoEvents。根据用户需求,用户可能希望添加延迟而不冻结界面,因此推荐第二种方法。下面给出两种方法的完整示例:方法1:使用Sleep(简单,但会冻结界面) ```vba'在模块顶部声明DeclareSub SleepLib "kernel32" (ByValdwMilliseconds AsLong)Sub TestSleep()MsgBox"等待5秒开始"Sleep5000'5秒MsgBox"等待结束" EndSub```方法2:使用timeGetTime(保持界面响应)```vba'在模块顶部声明PrivateDeclareFunction timeGetTimeLib "winmm.dll"() AsLongSub TestDelay()MsgBox"等待5秒开始,但你可以操作界面"Delay5000'调用自定义延迟函数,延迟5秒MsgBox "等待结束" EndSubSub Delay(ms AsLong)DimstartTimeAs LongstartTime= timeGetTimeDo WhiletimeGetTime <startTime+ msDoEvents'重要:让出控制权,以便处理其他事件LoopEndSub```注意:在方法2中,我们使用了一个循环,不断检查是否已经达到指定的时间,同时使用DoEvents来允许Excel处理其他事件(如用户输入)。这样,在等待期间用户仍然可以操作Excel。相关问题:1.在VBA中,除了Sleep和timeGetTime,还有哪些方法可以实现延迟?2.使用DoEvents有什么注意事项?3.如何在延迟期间允许用户取消操作?</think>### Excel VBA 添加延迟 Sleep 函数的方法 在 Excel VBA 中实现延迟/休眠功能,可通过以下两种主流方法实现: #### 方法 1:使用 Windows APISleep 函数(推荐) ```vba ' 在模块顶部声明 API Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Sub Example() MsgBox "开始执行" Sleep 2000 ' 暂停 2 秒(2000毫秒) MsgBox "2秒后继续" End Sub ``` **特点**: - 精确毫秒级延迟(参数单位为毫秒) - 执行期间会冻结 Excel 界面[^2] - 需配合 `DoEvents` 避免界面假死(见方法2优化) --- #### 方法 2:使用 timeGetTime + DoEvents(响应式延迟) ```vba ' 在模块顶部声明 Private Declare Function timeGetTime Lib "winmm.dll" () As Long Sub ResponsiveDelay(ms As Long) Dim startTime As Long startTime = timeGetTime() Do While timeGetTime() < startTime + ms DoEvents ' 关键:允许处理其他事件 Loop End Sub Sub Example() MsgBox "开始响应式延迟" ResponsiveDelay 3000 ' 延迟3秒 MsgBox "延迟结束,界面未冻结" End Sub ``` **优势**: - 延迟期间可响应界面操作[^2] - 避免 Excel 显示"未响应" - 适合需要用户交互的场景 --- ### 关键注意事项 1. **单位换算**: `Sleep 1000` = 1秒延迟(1000毫秒) 2. **API 声明位置**: 需放在模块的**顶部**(所有过程之前) 3. **DoEvents 的作用**: 允许系统处理其他事件(如按钮点击、界面刷新),防止界面卡死[^2] 4. **替代方案**: ```vba Application.Wait Now + TimeValue("00:00:02") ' 延迟2秒 ``` - 优点:无需 API 声明 - 缺点:精度低(最小1秒)、冻结界面 > ⚠️ 重要提示:使用 `Sleep` 时若省略 `DoEvents`,可能导致 Excel 界面暂时无响应[^2]。对于需要后台运行或用户交互的场景,优先选择 **方法2**。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值