The subject says it all. Specifically, say I created Channels this way.
Channel<byte[]> Stream { get; set; }
void Create()
{
Stream = System.Threading.Channels.Channel.CreateUnbounded<byte[]>();
}
Question
Is it ok to dispose it in the following order?
public virtual void Dispose()
{
if (Stream.Writer.TryComplete()) // Dispose writer
{
Stream.Reader.Completion.ContinueWith(o => // Dispose reader
{
// Dispose the rest, e.g. some streaming server
}).Unwrap();
}
}
P.S. In general, it works, but I'd like to make sure that if this code will be executed multiple times it won't cause memory leaks or unexpected runtime exceptions.