public void EnumerationCleaning() { var c = new WeakCollection <object>(); object x = new object(); using (NoGCRegion.Enter(1000)) { c.Add(x); c.Add(x); c.Add(x); AddCollectableItems(c, 3); Assert.AreEqual(6, c.AddCountSinceLastClean); Assert.AreEqual(6, c.UnsafeCount); } Helpers.CollectAndWait(); foreach (object o in c) { } Assert.IsTrue(c.Remove(x)); Assert.IsTrue(c.Remove(x)); #if NETCOREAPP2_2 // NS2.0 target does not support removing stale entries as items are encountered. Assert.AreEqual(6, c.AddCountSinceLastClean); Assert.AreEqual(4, c.UnsafeCount); #else Assert.AreEqual(0, c.AddCountSinceLastClean); Assert.AreEqual(1, c.UnsafeCount); #endif GC.KeepAlive(x); }
private static void UpdateScrollBarStyles(FrameworkElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } var themeScrollBars = GetThemeScrollBars(element); if (themeScrollBars.HasValue) { SetScrollBarStyles(element, themeScrollBars.Value); if (themeScrollBars.Value) { WeakUnthemedScrollingElements.Remove(element); } else { WeakUnthemedScrollingElements.Add(element); } } else { RemoveScrollBarStyles(element); WeakUnthemedScrollingElements.Remove(element); } }
public void RemoveWorks() { var arr = new[] { "a", "b", "c", "d", "c" }; var wc = new WeakCollection <string>(arr); Assert.True(wc.Remove("c")); Assert.True(wc.OrderBy(x => x).SequenceEqual(new[] { "a", "b", "c", "d" })); Assert.True(wc.Remove("c")); Assert.True(wc.OrderBy(x => x).SequenceEqual(new[] { "a", "b", "d" })); Assert.False(wc.Remove("c")); Assert.True(wc.OrderBy(x => x).SequenceEqual(new[] { "a", "b", "d" })); }
public void RemoveWorksAfterCollect() { var arr = new[] { "a", "b" }; var wc = new WeakCollection <string>(arr); { wc.Add(new string('c', 1)); wc.Add(new string('d', 1)); wc.Add(new string('c', 1)); } GCEx.CollectAndWait(); Assert.False(wc.Remove("c")); Assert.False(wc.Remove("d")); Assert.True(wc.SequenceEqual(new[] { "a", "b" })); }
/// <summary> /// Unregisters the specified resource pool. /// </summary> /// <param name="pool">The resource pool to unregister.</param> internal static void Unregister(ResourcePool pool) { lock (PoolsInternal) { PoolsInternal.Remove(pool); } }
public void WeakCollection() { "添加元素,但只有 1 个被强引用,于是 GC 后只剩 1 个元素。".Test(() => { // Arrange var collection = new WeakCollection <object>(); // Action collection.Add(new object()); var a = new object(); collection.Add(a); GC.Collect(); // Assert Assert.AreEqual(1, collection.TryGetItems(x => true).Length); }); "移除元素,被强引用的元素也被移除,于是 GC 后没有元素了。".Test(() => { // Arrange var collection = new WeakCollection <object>(); // Action collection.Add(new object()); var a = new object(); collection.Add(a); collection.Remove(a); GC.Collect(); // Assert Assert.AreEqual(0, collection.TryGetItems(x => true).Length); }); }
public void WeakCollection() { "添加元素,但只有 1 个被强引用,于是 GC 后只剩 1 个元素。".Test(() => { // Arrange var collection = new WeakCollection <object>(); // Action AddNewObject(collection); var a = AddNewObjectAndReturn(collection); GC.Collect(); // Assert Assert.AreEqual(1, collection.TryGetItems(x => true).Length); // 必须在验证之后再使用一下变量,否则可能被提前回收。 Console.WriteLine(a); }); "移除元素,被强引用的元素也被移除,于是 GC 后没有元素了。".Test(() => { // Arrange var collection = new WeakCollection <object>(); // Action AddNewObject(collection); var a = AddNewObjectAndReturn(collection); collection.Remove(a); GC.Collect(); // Assert Assert.AreEqual(0, collection.TryGetItems(x => true).Length); }); }
public void BucketInCollectionReusedAfterRemove() { var _0 = new object(); var _1 = new object(); var _2 = new object(); _collection.Add(_0); _collection.Add(_1); _collection.Add(_2); GC.Collect(); _collection.Remove(_0); _collection.Remove(_1); _collection.Add(new object()); Assert.Equal(3, _collection.Size); Assert.Equal(2, _collection.Count()); }
/// <inheritdoc/> protected override void Dispose(bool disposing, bool disposeData) { if (!IsDisposed) { if (disposing) { InvalidateViewDependentData(); // Unregister camera node from global list. Instances.Remove(this); } base.Dispose(disposing, disposeData); } }
internal void Remove(ScaledTexture scaledTexture, Texture2D texture) { try { lock (texture.Meta()) { var Map = texture.Meta().SpriteTable; using (Lock.Exclusive) { try { ScaledTextureReferences.Purge(); var removeElements = new List <ScaledTexture>(); foreach (var element in ScaledTextureReferences) { if (element == scaledTexture) { removeElements.Add(element); } } foreach (var element in removeElements) { ScaledTextureReferences.Remove(element); } } catch { } Map.Clear(); } } } finally { if (scaledTexture.Texture != null && !scaledTexture.Texture.IsDisposed) { Debug.TraceLn($"Disposing Active HD Texture: {scaledTexture.SafeName()}"); //scaledTexture.Texture.Dispose(); } } }
private void Unsubscribe(IObserver <T> observer) { lock (_gate) _observers?.Remove(observer); }
public static void UnregisterMainToolBarTray(ToolBarTray tray) { MainToolBarTrays.Remove(tray); }