当前位置: 首页>>代码示例>>C#>>正文


C# WeakReference.TryGetTarget方法代码示例

本文整理汇总了C#中WeakReference.TryGetTarget方法的典型用法代码示例。如果您正苦于以下问题:C# WeakReference.TryGetTarget方法的具体用法?C# WeakReference.TryGetTarget怎么用?C# WeakReference.TryGetTarget使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WeakReference的用法示例。


在下文中一共展示了WeakReference.TryGetTarget方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetValueTest

    public static void GetValueTest()
    {
        ConditionalWeakTable<object, object> cwt = new ConditionalWeakTable<object, object>();
        object key = new object();
        object obj = null;

        object value = cwt.GetValue(key, k => new object());

        Assert.True(cwt.TryGetValue(key, out value));
        Assert.Equal(value, cwt.GetOrCreateValue(key));

        WeakReference<object> wrValue = new WeakReference<object>(value, false);
        WeakReference<object> wrkey = new WeakReference<object>(key, false);
        key = null;
        value = null;

        GC.Collect();

        // key and value must be collected
        Assert.False(wrValue.TryGetTarget(out obj));
        Assert.False(wrkey.TryGetTarget(out obj));
    }
开发者ID:nblumhardt,项目名称:corefx,代码行数:22,代码来源:ConditionalWeakTable.cs

示例2: TestNullTarget_CompletionNoCaching

        [OuterLoop] // finalizer/GC interactions
        public void TestNullTarget_CompletionNoCaching()
        {
            // Make sure that the Completion task returned by a NullTarget
            // is not cached across all NullTargets.  Since it'll never complete,
            // that would be a potentially huge memory leak.

            var state = new object();
            var wro = new WeakReference<object>(state);
            DataflowBlock.NullTarget<int>().Completion.ContinueWith(delegate { }, state);
            state = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            Assert.False(wro.TryGetTarget(out state));
        }
开发者ID:gitter-badger,项目名称:corefx,代码行数:18,代码来源:DataflowBlockExtensionTests.cs

示例3: AllowFinalization

        public void AllowFinalization()
        {
            // Explicitly do not dispose, to allow finalization to happen, just to try to verify
            // that nothing fails when it does.
            MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, 4096);
            MemoryMappedViewStream s = mmf.CreateViewStream();

            var mmfWeak = new WeakReference<MemoryMappedFile>(mmf);
            var sWeak = new WeakReference<MemoryMappedViewStream>(s);

            mmf = null;
            s = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            Assert.False(mmfWeak.TryGetTarget(out mmf));
            Assert.False(sWeak.TryGetTarget(out s));
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:20,代码来源:MemoryMappedViewStream.Tests.cs


注:本文中的WeakReference.TryGetTarget方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。