How to use System.Media.SoundPlayer to asynchronously play a sound file?

后端 未结 2 1727
再見小時候
再見小時候 2020-12-05 19:53

Here\'s a deceptively simple question:

What is the proper way to asynchronously play an embedded .wav resource file in Windows Forms?

Attemp

2条回答
  •  青春惊慌失措
    2020-12-05 20:02

    I don't have enough reputation to comment so I'll just answer.

    If your requirements to play sound are "deceptively simple" (you just want to play the occasional sound when a single winform user does something) then I would use Attempt #4 above.

    Larry Osterman's "what's wrong with this code part 26" has his "system" spin off a new threadpool thread (to play sound) with each keystroke. He indicates than hammering away on it saturated the default 500 thread pool size in about 15 seconds of typing but this was also with a client/server app using async RPC that were also using the threadpool. Really not a "deceptively simple" application.

    If you are trying to queue sound bytes every second (or faster) for 10s or 100s of seconds at a time then its really not a "simple application" and a queued threading/priority subsystem would probably be in order.

提交回复
热议问题