【发布时间】:2012-06-14 18:22:15
【问题描述】:
我有一个小的 Windows 窗体程序,它使用 console.beep 从字符串中播放音乐。我在一个新线程't'中设置了字符串播放位(基本上是一个for循环,逐个字符地遍历字符串并播放适当的音符)。当您点击“播放”按钮时,线程启动并且按钮变为“停止”按钮。如果您在播放音乐时点击这个“停止”按钮,它将停止并且按钮变回播放(通过调用“完成”方法。 我的问题是我希望在新线程中运行的循环在循环运行并且歌曲结束时也调用“完成”方法。 但是,如果我在循环之后放置finished(),我会得到“非静态字段需要对象引用”错误。如果我将“finshed”方法更改为静态,那么更改按钮文本的位不起作用...
这是按下按钮时的代码...
//This is the method for when the "start" button is clicked
public void button1_Click(object sender, EventArgs e)
{
if (music == null) { return; }
if (button1.Text == "Play")
{
// it makes a new thread which calls my "parse" method which
// interprets the music and then calls "playnote" to play it.
Thread t = new Thread(parse);
t.Start();
button1.Text = "Stop";
}
else
{
finished();
}
}
public void finished()
{
stop = true;
button1.Text = "Play";
}
有什么建议吗?
提前非常感谢!
编辑:谢谢大家!!我真的没有时间弄清楚后台工作人员 atm 所以我现在只有单独的启动和停止按钮! :p
【问题讨论】:
标签: c# multithreading visual-studio-2010 static