让我们说我有一个等待用户在推进之前点击按钮的主题:
System.Threading.AutoResetEvent dialoguePause = new System.Threading.AutoResetEvent(false);
public void AskQuestion()
{
/* buttons containing choices created here */
dialoguePause.WaitOne();
/*Code that handles choice here */
}
public void Choice_Clicked(object sender, EventArgs e)
{
dialoguePause.Set();
}
如何在不依赖类变量的情况下将来自线程的数据传递给Ask_Clicked到AskQuestion?我能做的最好的就是:
System.Threading.AutoResetEvent dialoguePause = new System.Threading.AutoResetEvent(false);
string mostRecentChoice;
public void AskQuestion()
{
/* buttons containing choices created here */
dialoguePause.WaitOne();
MessageBox.Show("You chose " + mostRecentChoice + ".");
}
public void Choice_Clicked(object sender, EventArgs e)
{
mostRecentChoice = (sender as Button).Content.ToString(); //Ugly!
dialoguePause.Set();
}
答案 0 :(得分:0)
有很多方法可以实现这一目标:
我想说的是,这取决于应用程序。我不知道这是WinForm,WPF还是Web ......