Move all event invocations outside locks
This commit is contained in:
parent
5a093a9a04
commit
508e031143
@ -85,13 +85,18 @@ namespace LibationWinForms.ProcessQueue
|
||||
|
||||
public bool RemoveQueued(T item)
|
||||
{
|
||||
bool itemsRemoved;
|
||||
int queuedCount;
|
||||
|
||||
lock (lockObject)
|
||||
{
|
||||
bool removed = _queued.Remove(item);
|
||||
if (removed)
|
||||
QueuededCountChanged?.Invoke(this, _queued.Count);
|
||||
return removed;
|
||||
itemsRemoved = _queued.Remove(item);
|
||||
queuedCount = _queued.Count;
|
||||
}
|
||||
|
||||
if (itemsRemoved)
|
||||
QueuededCountChanged?.Invoke(this, queuedCount);
|
||||
return itemsRemoved;
|
||||
}
|
||||
|
||||
public void ClearCurrent()
|
||||
@ -102,32 +107,33 @@ namespace LibationWinForms.ProcessQueue
|
||||
|
||||
public bool RemoveCompleted(T item)
|
||||
{
|
||||
bool itemsRemoved;
|
||||
int completedCount;
|
||||
|
||||
lock (lockObject)
|
||||
{
|
||||
bool removed = _completed.Remove(item);
|
||||
if (removed)
|
||||
CompletedCountChanged?.Invoke(this, _completed.Count);
|
||||
return removed;
|
||||
itemsRemoved = _completed.Remove(item);
|
||||
completedCount = _completed.Count;
|
||||
}
|
||||
|
||||
if (itemsRemoved)
|
||||
CompletedCountChanged?.Invoke(this, completedCount);
|
||||
return itemsRemoved;
|
||||
}
|
||||
|
||||
public void ClearQueue()
|
||||
{
|
||||
lock (lockObject)
|
||||
{
|
||||
_queued.Clear();
|
||||
QueuededCountChanged?.Invoke(this, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearCompleted()
|
||||
{
|
||||
lock (lockObject)
|
||||
{
|
||||
_completed.Clear();
|
||||
CompletedCountChanged?.Invoke(this, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Any(Func<T, bool> predicate)
|
||||
{
|
||||
@ -174,13 +180,18 @@ namespace LibationWinForms.ProcessQueue
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
int queuedCount, completedCount = 0;
|
||||
bool completedChanged = false;
|
||||
try
|
||||
{
|
||||
lock (lockObject)
|
||||
{
|
||||
if (Current != null)
|
||||
{
|
||||
_completed.Add(Current);
|
||||
CompletedCountChanged?.Invoke(this, _completed.Count);
|
||||
completedCount = _completed.Count;
|
||||
completedChanged = true;
|
||||
}
|
||||
if (_queued.Count == 0)
|
||||
{
|
||||
@ -190,10 +201,17 @@ namespace LibationWinForms.ProcessQueue
|
||||
Current = _queued[0];
|
||||
_queued.RemoveAt(0);
|
||||
|
||||
QueuededCountChanged?.Invoke(this, _queued.Count);
|
||||
queuedCount = _queued.Count;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (completedChanged)
|
||||
CompletedCountChanged?.Invoke(this, _completed.Count);
|
||||
QueuededCountChanged?.Invoke(this, _queued.Count);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryPeek(out T item)
|
||||
{
|
||||
@ -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)
|
||||
{
|
||||
int queueCount;
|
||||
lock (lockObject)
|
||||
{
|
||||
_queued.AddRange(item);
|
||||
QueuededCountChanged?.Invoke(this, _queued.Count);
|
||||
queueCount = _queued.Count;
|
||||
}
|
||||
QueuededCountChanged?.Invoke(this, queueCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user