Is it a good practice to pass IDisposable as a parameter to a method and dispose it inside that method. This is sort of inevitable when you have to use several threads. Well, the best practices says the owner(caller) should dispose it.
E.g.
public void MyMethod(MyClass reader){
using(reader){
//some code
}
}
What if the owner (creating thread) no longer exist? E.g.
interface IReader : IDisposable {
string Read();
}
public class MyReader : IReader {
public string Read()
{
return "hellow world";
}
public void Dispose()
{
//dispose code
}
}
Here you find the problem...
public void Start() {
MyReader[] readerSet = new MyReader[5];
for (int i = 0; i < readerSet.Length; i++) {
readerSet[i] = new MyReader();
}
foreach (IReader reader in readerSet) {
ThreadPool.QueueUserWorkItem(new WaitCallback(Run), reader);
}
//exit after creating threads
}
public void Run(Object objReader) {
IReader reader = (IReader)objReader;
using (reader) {
//use the reader
}
}
I think you would be better off taking a creation delegate to guarantee disposal of the object.
Example
public void Start() {
var makeReader = new Func<IReader>(() => new MyReader());
for (int i = 0; i < 5; i++) {
ThreadPool.QueueUserWorkItem(Run, makeReader);
}
}
public void Run(Object state) {
var makeReader = (Func<IReader>)state;
using (var reader = makeReader()) {
//use the reader
}
}