Note that there are some explanatory texts on larger screens.

plurals
  1. POlock statement does not appear to be working
    primarykey
    data
    text
    <p>I have this method:</p> <pre><code>public bool Remove(EntityKeyType key) { lock (syncroot) { //wait if we need to waitForContextMRE.Wait(); //if the item is not local, assume it is not remote. if (!localCache.ContainsKey(key)) return false; //build an expression tree Expression&lt;Func&lt;EntityType, bool&gt;&gt; keyComparitorExpression = GenerateKeyComparitorExpression(key); var itemToDelete = TableProperty.Single(keyComparitorExpression); //delete from db TableProperty.DeleteOnSubmit(itemToDelete); DataContext.SubmitChanges(); //get the removed item for OnCollectionChanged EntityType itemToRemove = localCache[key]; itemToRemove.PropertyChanged -= item_PropertyChanged; //remove from the list Debug.Assert(localCache.Remove(key)); //call the notification OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, itemToRemove)); return true; } } </code></pre> <p>I am calling it from multiple threads (calling the same instance), but an exception keeps being thrown on TableProperty.Single (Sequence contains no elements). Upon debugging the code I saw that a situation is being created where the item is being deleted from the database after a different thread has checked the cache for its existence. This should not be possible unless there are multiple threads inside the lock statement (The syncroot object is definitely the same instance across threads).</p> <p>Impossible? I have proof: <img src="https://i.stack.imgur.com/ZI8a4.png" alt="Impossible situation"></p> <p>There are three threads inside the lock statement! What gives?</p> <p>notes:</p> <ol> <li>The MRE is set (not blocking).</li> <li>This is not a situation where the exception gets thrown, it just shows multiple threads inside a lock section. <strong>Update</strong>: I changed the image to an intellitrace event of the exception. The old image is <a href="https://i.stack.imgur.com/ZdIvB.png" rel="nofollow noreferrer">here</a></li> <li>The syncroot object is not static, because I only want calls to the same instance syncronized.</li> </ol> <p><strong>Update</strong></p> <p>This is the declaration of the syncroot object:</p> <pre><code>private object syncroot = new object(); </code></pre> <p>And some other declarations:</p> <pre><code>private ManualResetEventSlim waitForContextMRE = new ManualResetEventSlim(true); private DataContextType _dataContext; private System.Data.Linq.Table&lt;EntityType&gt; _tableProperty; //DataContextType and EntityType are generic type parameters </code></pre> <p>I cannot make the syncroot static because I have several instances of the class running and it is important that they don't block each other. But that doesn't really matter - making it static does not fix the problem.</p> <p>The ManualResetEvent (waitForContextMRE) is not there for synchronization - it's there to block database operations for a certain time after certain operations are performed (i.e. on startup). It is set most of the time. Taking it out of the lock block also does not fix the problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload