Skip to content

Instantly share code, notes, and snippets.

@mr5z
Created April 26, 2021 11:18
Show Gist options
  • Save mr5z/98c5c8496fe4063d752c5443dc1f3c72 to your computer and use it in GitHub Desktop.
Save mr5z/98c5c8496fe4063d752c5443dc1f3c72 to your computer and use it in GitHub Desktop.
Scheduled disposable objects to be registered in an IoC container where IDisposable is disallowed.
private void ScheduleDisposeResourceStreams()
{
_ = Task.Factory.StartNew(async () =>
{
await Task.Delay(TimeSpan.FromMinutes(1));
// `stream` creation happens right after the invocation of this
// They are in the same thread so no lock needed
jsonStream?.Dispose();
jsonDocument?.Dispose();
jsonStream = null;
jsonDocument = null;
}, default, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment