我有这样的代码:
public void RangeMissing(/*params*/)
{
Task.Factory.StartNew(() =>
{
....
bool needReceive = true;
while (needReceive) {
....
lock (lockObj) {
....
// calling third party dll method that calls MdrResponseInterpreter in its turn
}
}
}
}
public bool MdrResponseInterpreter(DNMessageDeliverer builder, DNFieldSet message)
{
.....
// i need to update needReceive here
needReceive = false;
}
可能正在同时执行多项任务。我需要从needReceive
Task
的{{1}}变量
答案 0 :(得分:0)
如果你像在这里一样锁定它们,几个任务将无法同时运行
lock (lockObj) {
MdrResponseInterpreter方法的声明显示它是一个实例方法 - 只需使needReceive成员并锁定该实例。