你在正确的轨道上。但是,我建议在从方法返回之前等待任务终止Dispose
,以避免在处理对象后任务继续运行的竞争条件。还处置CancellationTokenSource
.
更新:如果您使用的是 .NET Core 3.0 或更高版本,您应该让您的类实现IAsyncDisposable
并等待DisposeAsyncCore
方法中的任务。我更新了下面的示例以反映这一点。
using System;
using System.Threading;
using System.Threading.Tasks;
public class MyClass : IDisposable, IAsyncDisposable
{
private readonly CancellationTokenSource feedCancellationTokenSource =
new CancellationTokenSource();
private readonly Task feedTask;
public MyClass()
{
feedTask = Task.Factory.StartNew(() =>
{
while (!feedCancellationTokenSource.IsCancellationRequested)
{
// do finite work
}
});
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
feedCancellationTokenSource.Cancel();
feedTask.Wait();
feedCancellationTokenSource.Dispose();
feedTask.Dispose();
}
}
public async ValueTask DisposeAsync()
{
await DisposeAsyncCore().ConfigureAwait(false);
Dispose(false);
GC.SuppressFinalize(this);
}
protected virtual async ValueTask DisposeAsyncCore()
{
feedCancellationTokenSource.Cancel();
await feedTask.ConfigureAwait(false);
feedCancellationTokenSource.Dispose();
feedTask.Dispose();
}
}
// Sample usage:
public static class Program
{
public static async Task Main()
{
await using (new MyClass())
{
// do something else
}
Console.WriteLine("Done");
}
}