Search code examples
pythonpython-3.xtimesleep

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. I know that winsound.Beep allows me to specify the time in milliseconds, but how can I implement that behavior with winsound.PlaySound?

I tried to use the time.sleep function, but that only delays the function, not specifies the amount of time.

Any help would be appreciated.


Solution

  • 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.