我这个代码有问题。该功能正在播放音乐曲目,因此需要一段时间才能完成执行....但是,即使通过了它,它也不会返回,直到它完成,继续执行其余的程序。我可以有功能退出,让程序继续,但音乐保持它自己的线程。欢迎任何解决方案。在c#中执行函数的操作不起作用
using System;
using Gtk;
using NAudio;
using NAudio.Wave;
using System.Threading;
public class Trackbox {
public static void Main() {
Application.Init();
//Create the Window
Window myWin = new Window("Trackbox");
myWin.SetIconFromFile("Assets//logo.png");
myWin.Resize(200, 100);
//Add the label to the form
//myWin.Add(myLabel);
Button playButton = new Button("Play Sound");
//This when playwav is called here, the rest of the application waits for it to finish playing
playButton.Clicked += new EventHandler(playWav);
myWin.Add(playButton);
myWin.DeleteEvent += delegate { Application.Quit(); };
//Show Everything
myWin.ShowAll();
Application.Run();
}
private static void playWav(object sender, EventArgs e)
{
var soundFile = @"C:\sound.wav";
using (var wfr = new WaveFileReader(soundFile))
using (WaveChannel32 wc = new WaveChannel32(wfr) { PadWithZeroes = false })
using (var audioOutput = new DirectSoundOut())
{
audioOutput.Init(wc);
audioOutput.Play();
while (audioOutput.PlaybackState != PlaybackState.Stopped)
{
Thread.Sleep(20);
}
audioOutput.Stop();
}
}
}
感谢您的帮助。如果您有任何想法,请发布。
非常感谢,这解释了线程如何工作得更好! – 2013-05-11 21:22:10
事后如何阻止胎面?现在我不能杀死它,因为线程处于静态函数。 – 2013-05-11 21:29:22
这个函数是否是静态的并不重要。你应该看到一个线程是在“主进程”旁边运行的一个单独的“进程”。它们并排运行并执行各种代码(静态,非静态,创建实例等)。停止线程的最好方法是设置一个标志。我会更新我的答案。 – 2013-05-11 22:52:47