Note that there are some explanatory texts on larger screens.

plurals
  1. POSynchronizing with an object while using the Task Parallel Library
    text
    copied!<p>I have a WPF app that from time to time needs to perform a long-running operation - or rather, many small operations that in sum total take a while. I have found that the Task Parallel Library in .Net 4 works fine for this.</p> <p>However, this operation by nature should run to completion before another of the same kind is started. And there is a very real chance that the user could perform an action that requires the process to run, even while the last one is still going. I would like to synchronize this so that only one is ever running at a time. When the running instance completes, another gains the locks and goes for it, etc. until there are no more of these to run.</p> <p>I have a class that runs the process named EntityUpdater. In this class I thought it would be clever to define a syncronization object:</p> <pre><code>private static object _lockObject = new object(); </code></pre> <p>Making it static should ensure that any EntityUpdater object will await its turn as long as the locking is correct, right?</p> <p>So my naive first attempt did this before starting the Task (which in turns starts all the other little child tasks, attached to their parent):</p> <pre><code>Monitor.Enter(_lockObject, ref _lockAquired); </code></pre> <p>(_lockAquired is just a local bool)</p> <p>The main task (the one with all the child tasks) has a continuation, which exists more or less only to do</p> <pre><code>Monitor.Exit(_lockObject); </code></pre> <p>I know I should put this in a finally, but it is pretty much the only code in the continuation, so I don't see how that would make a difference.</p> <p>Anyway, I assume that there is some threading voodoo here that causes me to get the "Object synchronization method was called from an unsynchronized block of code" SynchronizationLockException. I have made sure that _lockAquired is actually true, and I have tried to Monitor.Enter in several different places, but I always get this.</p> <p>So, basically, my question is how I can synchronize access to an object (the object itself is not important) so that only one copy of the process is running at any given time, and any others that may be started while one is already running will block? The complication - I guess - appears with the added demand that the lock should be released at some time in the future, when all the child tasks of the first TPL Task are complete.</p> <p><strong>UPDATE</strong></p> <p>Here is some code that shows what I am doing right now.</p> <pre><code>public class EntityUpdater { #region Fields private static object _lockObject = new object(); private bool _lockAquired; private Stopwatch stopWatch; #endregion public void RunProcess(IEnumerable&lt;Action&lt;IEntity&gt;&gt; process, IEnumerable&lt;IEntity&gt; entities) { stopWatch = new Stopwatch(); var processList = process.ToList(); Monitor.Enter(_lockObject, ref _lockAquired); //stopWatch.Start(); var task = Task.Factory.StartNew(() =&gt; ProcessTask(processList, entities), TaskCreationOptions.LongRunning); task.ContinueWith(t =&gt; { if(_lockAquired) Monitor.Exit(_lockObject); //stopWatch.Stop(); }); } private void ProcessTask(List&lt;Action&lt;IEntity&gt;&gt; process, IEnumerable&lt;IEntity&gt; entities) { foreach (var entity in entities) { var switcheroo = entity; // To avoid closure or whatever Task.Factory.StartNew(() =&gt; RunSingleEntityProcess(process, switcheroo), TaskCreationOptions.AttachedToParent); } } private void RunSingleEntityProcess(List&lt;Action&lt;IEntity&gt;&gt; process, IEntity entity) { foreach (var step in process) { step(entity); } } } </code></pre> <p>As you can see, it is not complicated, and this is also probably far from production worthy - just an attempt that shows what I can't get to work.</p> <p>The exception I get is of course in the Monitor.Exit() call in the task continuation.</p> <p>I hope that makes this a bit clearer.</p>
 

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