I think Monitor can archieve your Requirement , check whether it make a conflict with Forms.Timer ? the timer is STA
here 's a sample you can trylock(this)
{
if (!readerFlag)//如果现在不可读取
{
try
{
file://等待WriteToCell方法中调用Monitor.Pulse()方法
Monitor.Wait(this);
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
Console.WriteLine("Consume: {0}",cellContents);
readerFlag = false; file://重置readerFlag标志,表示消费行为已经完成
Monitor.Pulse(this); file://通知WriteToCell()方法(该方法在另外一个线程中执行,等待中)
}
return cellContents;
}
here 's a sample you can trylock(this)
{
if (!readerFlag)//如果现在不可读取
{
try
{
file://等待WriteToCell方法中调用Monitor.Pulse()方法
Monitor.Wait(this);
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
Console.WriteLine("Consume: {0}",cellContents);
readerFlag = false; file://重置readerFlag标志,表示消费行为已经完成
Monitor.Pulse(this); file://通知WriteToCell()方法(该方法在另外一个线程中执行,等待中)
}
return cellContents;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货