-1

I have such code:

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;
}

Several task may be executing at the same time. I need to update needReceive variable of corresponding Task from MdrResponseInterpreter

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305

1 Answers1

0

Several tasks will not run at the same time if you lock them as you do here

lock (lockObj) {

the MdrResponseInterpreter method's declaration reveals that it's an instance method - just make needReceive a member and lock on that instance.

Ventsyslav Raikov
  • 6,882
  • 1
  • 25
  • 28
  • I want several tasks to run at the same time. I lock only small part of the entire task. – Oleg Vazhnev Mar 28 '12 at 13:01
  • ok, so just put the needReceive on the object which has the MdrResponseInterpreter method then – Ventsyslav Raikov Mar 28 '12 at 13:07
  • DNMessageDeliverer has MdrResponseInterpreter method but I'm using one DNMessageDeliverer in several tasks so this will not work because when I will set `needReceive` to true in one task I will updated other tasks... But If I will be inside "lock (lockObj" this should work I just will need to copy `needReceive` from `DNMessageDeliverer` to outer class... however i'm not sure if that would be very nice approach – Oleg Vazhnev Mar 28 '12 at 13:11