What would be the best pattern to implement for a class with a long-running method and a method that releases resources. Currently, it looks like this:
static async Task<int> Main(string[] args)
{
MyProcessor processor = new MyProcessor();
processor.SomeConfigOptions = ...;
processor.SomeEvent += ...;
// ProcessAsync() method contains an infinite loop that runs until we signal to stop.
// ProcessAsync() method allocates unmanaged resourses.
using (Task engineTask = Task.Run(() => processor.ProcessAsync()))
{
// Keep application running some event.
Console.ReadKey();
// Signal to stop processing, release unmanaged resources, and exit ProcessAsync() method.
processor.Stop();
// Wait for the processing to finish and all unmanaged resources are released.
processor.Wait();
}
return 1;
}
Does it make sense to derive MyProcessor from IDisposable? Or to return a class derived from Task from ProcessAsync() method? In both cases, the Stop() method will be called inside IDisposable.Dispose() override. Any better approach for this pattern?
Aucun commentaire:
Enregistrer un commentaire