gpt4 book ai didi

c# - 对AsyncLazy的处置,怎样才是正确的(好用不漏)的方式?

转载 作者:太空狗 更新时间:2023-10-29 21:44:34 27 4
gpt4 key购买 nike

我正在使用 Stephen Cleary's AsyncLazy implementation, from his blog. 的特化

/// <summary>
/// Provides support for asynchronous lazy initialization.
/// This type is fully thread-safe.
/// </summary>
/// <typeparam name="T">
/// The type of object that is being asynchronously initialized.
/// </typeparam>
public sealed class AsyncLazy<T>
{
/// <summary>
/// The underlying lazy task.
/// </summary>
private readonly Lazy<Task<T>> instance;

/// <summary>
/// Initializes a new instance of the
/// <see cref="AsyncLazy&lt;T&gt;"/> class.
/// </summary>
/// <param name="factory">
/// The delegate that is invoked on a background thread to produce
/// the value when it is needed.
/// </param>
/// <param name="start">
/// If <c>true</c> commence initialization immediately.
/// </param>
public AsyncLazy(Func<T> factory, bool start = false)
{
this.instance = new Lazy<Task<T>>(() => Task.Run(factory));
if (start)
{
this.Start();
}
}

/// <summary>
/// Initializes a new instance of the
/// <see cref="AsyncLazy&lt;T&gt;"/> class.
/// </summary>
/// <param name="factory">
/// The asynchronous delegate that is invoked on a background
/// thread to produce the value when it is needed.
/// </param>
/// <param name="start">
/// If <c>true</c> commence initialization immediately.
/// </param>
public AsyncLazy(Func<Task<T>> factory, bool start = false)
{
this.instance = new Lazy<Task<T>>(() => Task.Run(factory));
if (start)
{
this.Start();
}
}

/// <summary>
/// Asynchronous infrastructure support.
/// This method permits instances of
/// <see cref="AsyncLazy&lt;T&gt;"/> to be await'ed.
/// </summary>
public TaskAwaiter<T> GetAwaiter()
{
return this.instance.Value.GetAwaiter();
}

/// <summary>
/// Starts the asynchronous initialization,
/// if it has not already started.
/// </summary>
public void Start()
{
var unused = this.instance.Value;
}
}

这是很棒的代码,我非常感谢它的易用性。即

class SomeClass
{
private readonly AsyncLazy<Thing> theThing = new AsyncLazy<Thing>(
() => new Thing());

void SomeMethod()
{
var thing = await theThing;
// ...
}
}

现在我的问题,

假设SomeClass继承自实现 IDisposable 的类那Thing工具 IDisposable .我们会有这样的框架实现,

class SomeClass : SomeDisposableBase
{
private readonly AsyncLazy<Thing> theThing = new AsyncLazy<Thing>(
() => new Thing());

protected override void Dispose(bool disposing)
{
if (disposing)
{
// What do I do with theThing?
}

base.Dispose(disposing);
}
}

那么,我该如何处理 theThingDispose覆盖?我应该延长 AsyncLazy<T> 吗?拥有新属性(property)?

// ...

public bool IsStarted
{
get
{
return this.instance.IsValueCreated;
}
}

// ...

我应该更改 AsyncLazy<T> 吗?实现IDisposable

我是不是误会了,我不需要担心?

我应该做点别的吗?

最佳答案

Stephen Toub's version of this class继承自 Lazy<Task<T>> , 所以你得到 IsValueCreated属性自动。

或者,您可以公开 IsValueCreated来自私有(private)领域的属性(property):

public sealed class AsyncLazy<T>
{
private readonly Lazy<Task<T>> instance;
...
public bool IsValueCreated
{
get { return instance.IsValueCreated; }
}
}

为了与内置 Lazy<T> 保持一致类型,我会避免将该属性重命名为 IsStarted .

关于c# - 对AsyncLazy的处置,怎样才是正确的(好用不漏)的方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24578185/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com