ReadOnlyList fetch with lock

ReadOnlyList fetch with lock

Old forum URL: forums.lhotka.net/forums/t/12725.aspx


MichelRenaud posted on Tuesday, August 19, 2014

Hi,

This morning I was upgrading CSLA 4.3 to 4.5 and when I ran our application, anything using a ReadOnlyList failed in the DataPortal_Fetch.  I finally found the answer in this thread: http://forums.lhotka.net/forums/p/12449/56992.aspx

OK, easy fix alright.  Except than I ran into some slightly different fetch code.  My boss had been working on making a data loader use threads to speed up the process.  But on occasion, it would fail because a thread could be trying to read a list while one was writing to it (can't remember the exact details, as I wasn't directly working on this).  So we have a number of lists where the DataPortal_Fetch does this:

while (dataReader.Read())
{
    currentObject = Word.GetWord(dataReader);
    lock (loadingLock)
    {
        this.Add(currentObject);
    }
}

I'm trying to figure out whether I should put the "this.ReadOnly = false" and "... = true" code inside the lock, outside the lock but inside the while, or before/after the while.

Or is the above a big no-no to begin with?

Thanks,
Michel 

JonnyBee replied on Tuesday, August 19, 2014

Hi,

Do you run asynchronous code in the DataPortal_Fetch method? 

If you do not run async code then you only need to ReadOnly = false on the first line in DataPortal_Fetch and reset to True before you leave the method. 

MichelRenaud replied on Wednesday, August 20, 2014

Nothing async started in DataPortal_Fetch, no.  It simply calls a stored procedure and adds the results to the list.

Thanks!
Michel 

Copyright (c) Marimer LLC