Note that there are some explanatory texts on larger screens.

plurals
  1. PORestart a Running Thread
    text
    copied!<p>I'm new to threading and need a lot of help. I have code based on events that starts a thread. The problem is that I loose references to the thread and thread.abort(); does not stop it, therefore my application doesn't stop. Thanks </p> <pre><code>using System; using Gtk; using NAudio; using NAudio.Wave; using System.Threading; public class Trackbox { public static Thread thread; public static void Main() { Application.Init(); //Create the Window Window myWin = new Window("Trackbox"); myWin.SetIconFromFile("Assets//logo.png"); myWin.Resize(200, 100); Button playButton = new Button("Play Sound"); playButton.Clicked += new EventHandler(playWav); myWin.Add(playButton); myWin.DeleteEvent += delegate { exitTrackbox(); }; //Show Everything myWin.ShowAll(); Application.Run(); } private static void exitTrackbox() { //Doesn't kill the application thread.Abort(); Application.Quit(); } private static void playWav(object sender, EventArgs e) { //Reference to Thread thread = new Thread(playWavThread); thread.Start(); } private static void playWavThread() { var soundFile = @"C:\sound.wav"; using (var wfr = new WaveFileReader(soundFile)) using (WaveChannel32 wc = new WaveChannel32(wfr) { PadWithZeroes = false }) using (var audioOutput = new WaveOut()) { audioOutput.Init(wc); audioOutput.Play(); while (audioOutput.PlaybackState != PlaybackState.Stopped) { Thread.Sleep(20); } audioOutput.Stop(); } } } </code></pre> <p>Please give any advice you have about this situation. Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload