简体   繁体   English

如何在一定时间内运行代码?

[英]How can I run code for a certain amount of time?

I want to play a sound (from a wav file) using winsound's winsound.PlaySound function.我想使用 winsound 的winsound.PlaySound函数播放声音(来自 wav 文件)。 I know that winsound.Beep allows me to specify the time in milliseconds, but how can I implement that behavior with winsound.PlaySound ?我知道winsound.Beep允许我以毫秒为单位指定时间,但是如何使用winsound.PlaySound实现该行为?

I tried to use the time.sleep function, but that only delays the function, not specifies the amount of time.我尝试使用time.sleep函数,但这只会延迟函数,而不是指定时间量。

Any help would be appreciated.任何帮助,将不胜感激。

Create a thread to play the sound, start it.创建一个线程来播放声音,启动它。 Create a thread that sleeps the right amount of time and has a handle to the first thread.创建一个睡眠时间适当的线程,并拥有第一个线程的句柄。 Have the second thread terminate the first thread when the sleep is over.当睡眠结束时,让第二个线程终止第一个线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM