I am trying to make multiple audio files playing simultaneously, so I decide to use thread to trigger them (otherwise the one can only start playing until the previous finished).
This is how I do it.
sounds is a list of SoundPlayers
for (int i = 0; i < sometimes; i++)
{
SoundPlayer sp = sounds[i];
Thread thread = new Thread(() =>
{
sp.PlayLooping();
});
threads.Add(thread);
}
foreach (Thread thread in threads)
{
thread.Start();
}
Then I use a timer to stop all the audio file after 1 second.
foreach(Thread t in threads)
{
kill_thread(t);
MessageBox.Show(t.IsAlive.ToString());
}
[SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
private void kill_thread(Thread t)
{
t.Abort();
}
I get kill_thread method from here. With the message pops up from messagebox, the target thread has already been killed in this way. However, the music is keep playing.
Is there any way to stop the music? Thanks in advance!