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
What does shat() do.
runs an audio file
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
this is shat


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
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.
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
can I see updateProgressbar1 code?
this is the point of the sleep
okay
( update zamzam is proressbar1)

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
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;
});
no its more of a sound effect
i will try tommorow