Monitor锁的问题
本帖最后由 cn_kap 于 2013-12-22 22:31:11 编辑
代码如下:
object o = new object();
private System.Speech.Synthesis.SpeechSynthesizer _SpeechSynthesizer = new System.Speech.Synthesis.SpeechSynthesizer();
private void button10_Click(object sender, EventArgs e)
{
Monitor.Enter(o);
_SpeechSynthesizer.SpeakAsyncCancelAll();
Monitor.Exit(o);
}
private void button9_Click(object sender, EventArgs e)
{
_SpeechSynthesizer.SpeakAsync("遵守中华人民共和国有关法律法规");
}
private void _SpeechSynthesizer_SpeakCompleted(object sender, System.Speech.Synthesis.SpeakCompletedEventArgs e)
{
Monitor.Exit(o);
}
private void _SpeechSynthesizer_SpeakStarted(object sender, System.Speech.Synthesis.SpeakStartedEventArgs e)
{
Monitor.Enter(o);
}
在SpeakStarted里加了锁,在Completed释放锁
现在是还没有释放锁的时候,button10的锁也能进去,
不知道是怎么回事,请高人解答下
------解决方案--------------------相同的线程是可以访问的。