Move all event invocations outside locks

This commit is contained in:
Michael Bucari-Tovo 2022-06-08 12:08:15 -06:00
parent 5a093a9a04
commit 508e031143

View File

@ -85,13 +85,18 @@ namespace LibationWinForms.ProcessQueue
public bool RemoveQueued(T item) public bool RemoveQueued(T item)
{ {
bool itemsRemoved;
int queuedCount;
lock (lockObject) lock (lockObject)
{ {
bool removed = _queued.Remove(item); itemsRemoved = _queued.Remove(item);
if (removed) queuedCount = _queued.Count;
QueuededCountChanged?.Invoke(this, _queued.Count);
return removed;
} }
if (itemsRemoved)
QueuededCountChanged?.Invoke(this, queuedCount);
return itemsRemoved;
} }
public void ClearCurrent() public void ClearCurrent()
@ -102,31 +107,32 @@ namespace LibationWinForms.ProcessQueue
public bool RemoveCompleted(T item) public bool RemoveCompleted(T item)
{ {
bool itemsRemoved;
int completedCount;
lock (lockObject) lock (lockObject)
{ {
bool removed = _completed.Remove(item); itemsRemoved = _completed.Remove(item);
if (removed) completedCount = _completed.Count;
CompletedCountChanged?.Invoke(this, _completed.Count);
return removed;
} }
if (itemsRemoved)
CompletedCountChanged?.Invoke(this, completedCount);
return itemsRemoved;
} }
public void ClearQueue() public void ClearQueue()
{ {
lock (lockObject) lock (lockObject)
{
_queued.Clear(); _queued.Clear();
QueuededCountChanged?.Invoke(this, 0); QueuededCountChanged?.Invoke(this, 0);
}
} }
public void ClearCompleted() public void ClearCompleted()
{ {
lock (lockObject) lock (lockObject)
{
_completed.Clear(); _completed.Clear();
CompletedCountChanged?.Invoke(this, 0); CompletedCountChanged?.Invoke(this, 0);
}
} }
public bool Any(Func<T, bool> predicate) public bool Any(Func<T, bool> predicate)
@ -175,23 +181,35 @@ namespace LibationWinForms.ProcessQueue
public bool MoveNext() public bool MoveNext()
{ {
lock (lockObject) int queuedCount, completedCount = 0;
bool completedChanged = false;
try
{ {
if (Current != null) lock (lockObject)
{ {
_completed.Add(Current); if (Current != null)
CompletedCountChanged?.Invoke(this, _completed.Count); {
} _completed.Add(Current);
if (_queued.Count == 0) completedCount = _completed.Count;
{ completedChanged = true;
Current = null; }
return false; if (_queued.Count == 0)
} {
Current = _queued[0]; Current = null;
_queued.RemoveAt(0); return false;
}
Current = _queued[0];
_queued.RemoveAt(0);
queuedCount = _queued.Count;
return true;
}
}
finally
{
if (completedChanged)
CompletedCountChanged?.Invoke(this, _completed.Count);
QueuededCountChanged?.Invoke(this, _queued.Count); QueuededCountChanged?.Invoke(this, _queued.Count);
return true;
} }
} }
@ -218,22 +236,15 @@ namespace LibationWinForms.ProcessQueue
} }
} }
public void Enqueue(T item)
{
lock (lockObject)
{
_queued.Add(item);
QueuededCountChanged?.Invoke(this, _queued.Count);
}
}
public void Enqueue(IEnumerable<T> item) public void Enqueue(IEnumerable<T> item)
{ {
int queueCount;
lock (lockObject) lock (lockObject)
{ {
_queued.AddRange(item); _queued.AddRange(item);
QueuededCountChanged?.Invoke(this, _queued.Count); queueCount = _queued.Count;
} }
QueuededCountChanged?.Invoke(this, queueCount);
} }
} }
} }