私はスレッド化が初めてで、多くの助けが必要です。スレッドを開始するイベントに基づくコードがあります。問題は、スレッドと 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();
}
}
}
この状況について何かアドバイスがあればお願いします。ありがとう