I sucks at thread, so please help me [Answered]

Zzonedetec10/15/2022
Hello guys

How can I execute a method without blocking the UI?
It's because when I click on keyboard key it supposed to play a sound using Window_KeyDown event but when I type to fast sounds doesnt play.
So how can I put the code that permit to play the sound into a thread things so it does not block the ui?
thanks

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            // Bruit de clavier mécanique
            Stream str;


            str = new StreamReader(
            System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("TypeSpone.resources.key1_enter_release.wav")!
                ).BaseStream;

            System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
            snd.Play();
        }
Mmrphil210510/15/2022
Maybe there is a way to play without a new thread?
Mmrphil210510/15/2022
I know NAudio has an asynchronous API
Mmrphil210510/15/2022
Also, why are you creating a StreamReader just to get the base stream that you passed to it?
Mmrphil210510/15/2022
You can just use the return value of GetManifestResourceStream directly
Mmrphil210510/15/2022
StreamReader is meant for text
Zzonedetec10/15/2022
oh thanks, it works too
Zzonedetec10/15/2022
I'm gonna read about it thanks
Zzonedetec10/15/2022
I think I'm gonna need some help ^^'
Zzonedetec10/15/2022
I don't think NAudio is a good idea
Zzonedetec10/15/2022
I don't want to install a new package in my project. Is it possible to stay with the thread method?
Mmrphil210510/15/2022
Why don't you want a new package?
Mmrphil210510/15/2022
That is a very common thing to do
Zzonedetec10/15/2022
Actually I don't know how to use NAudio and it seems complicated
Mmrphil210510/15/2022
Well it's def. not complicated
Mmrphil210510/15/2022
Just read their documentation
Mmrphil210510/15/2022
Or StackOverflow posts
Zzonedetec10/15/2022
okay then
Zzonedetec10/15/2022
and last thing, is there any more faster way to get the stream of my audio from the resources?
Zzonedetec10/15/2022
than this :
                str = System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("TypeSpone.resources.key1_enter_release.wav")!;
Mmrphil210510/15/2022
Is it slow?
Zzonedetec10/15/2022
Idk it feels slow, or maybe the SoundPlayer class is slow
Zzonedetec10/15/2022
let me try with NAudio then I tell you
Mmrphil210510/15/2022
Well it opens a stream that should give you bytes gradually
Mmrphil210510/15/2022
I don't think that'd be slow
Zzonedetec10/15/2022
do you suggest me mp3 or wav?
Zzonedetec10/15/2022
but NAudio works perfectly thanks 😄
Zzonedetec10/15/2022
I don't even need to use the Asynchronous thing
KKouhai10/15/2022
mp3 is lossy compressed, wav isn't.
So choosing between them depends on your requirements
Mmrphil210510/15/2022
NAudio supports both
Mmrphil210510/15/2022
Most people can't hear the difference anyway between a high quality MP3 and WAV
Zzonedetec10/15/2022
okay thanks a lot 🙂
AAccord10/15/2022
✅ This post has been marked as answered!