我是线程新手,需要很多帮助。我有基于启动线程的事件的代码。问题是我失去了对线程和 thread.abort(); 的引用。不会停止它,因此我的应用程序不会停止。谢谢
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();
}
}
}
请提供您对这种情况的任何建议。谢谢