bool ApplyOperation (int num, Action<int> doCheck, out int newValue) { int oldCount; newValue = 0; do { oldCount = count; if (oldCount == 0) return false; newValue = oldCount + num; if (doCheck != null) doCheck (newValue); } while (Interlocked.CompareExchange (ref count, newValue, oldCount) != oldCount); return true; }