Let's say I have this class Loggerthat writes strings to a low priority workflow that is not a background thread. Lines are queued Logger.WriteLineand flushed to Logger.Worker. No queues in the queue can be lost. Something like this (implementation, locking, synchronization, etc. Omitted for clarity):
public class Logger
{
private Thread workerThread;
private Queue<String> logTexts;
private AutoResetEvent logEvent;
private AutoResetEvent stopEvent;
public void WriteLine(String text);
public void AsyncStop();
private void Worker();
}
Since the worker thread is the front thread, I should be able to deterministically stop it if the process should be able to complete.
Question: Is the general recommendation in this scenario to allow workflow to be Loggerimplemented IDisposableand stopped in Dispose()? Something like that:
public class Logger : IDisposable
{
...
public void Dispose()
{
AsyncStop();
this.workerThread.Join();
}
}
?