C
C#3d ago
mustaf

why does the audio stop playing even on a seprate thread

Thread task = new Thread(() => { shat(); //runs an audio file using Naudio }); task.Start(); // Task task = new Task(() => //{ for (int i = 0; i < 500; i++) { Random Rnd = new Random(); double num = Rnd.Next(100); updateprogressbar1(num); int j = i / 5; updateProgressbar2(j); Thread.Sleep(10); } when i try to run this the audio plays for a split second but then cuts off , then plays again after the loop is complete
15 Replies
wasabi
wasabi3d ago
What does shat() do.
mustaf
mustafOP3d ago
runs an audio file
FusedQyou
FusedQyou3d ago
This code does not do anything relevant to audio so unless you share more code/context/errors there's no way to answer this You also should not use threads unless you have a specific reason to. Use async-await
mustaf
mustafOP3d ago
this is shat
No description
mustaf
mustafOP3d ago
No description
mustaf
mustafOP3d ago
i've been using dotnet for less than a month am still quite new so dont mind me if i forget to add som informatoin
Muhammad Ali
Muhammad Ali3d ago
1st of all why are using thread? 2nd where you placed your above code? inside program.cs or inside form? when you use Thread.sleep it sleeps the current thread. its mean if that thread is handling your audio, it will never work. it will work again after 10 seconds and I think that is happening in your scenario.
mustaf
mustafOP3d ago
its because am trying to fix the fact that the audio cuts off when the loop runs what am trying to do is i want to do like some animation with the progress bar
Muhammad Ali
Muhammad Ali3d ago
can I see updateProgressbar1 code?
mustaf
mustafOP3d ago
this is the point of the sleep
Muhammad Ali
Muhammad Ali3d ago
okay
mustaf
mustafOP3d ago
( update zamzam is proressbar1)
No description
mustaf
mustafOP3d ago
ignore the fact i renamed it the top most code is within the method activated by a button click the rest are within the same file
Muhammad Ali
Muhammad Ali3d ago
okay. Let me figure it out. okay, one more question. is shat producing music in a loop? until I press stop? I think you should try Task.Run(()=> shat()) If there is any sort of thread blockage it will handle by its own. same you can do for the updateProgressbar loop but you will might face an issue of cross threading. for that you have to use Invoke method of windows form to merge threading communication. like this.Invoke((MethodInvoker)delegate { progressBar1.Value = (int)num; });
mustaf
mustafOP2d ago
no its more of a sound effect i will try tommorow

Did you find this page helpful?