Is it good practice to pass IDisposable as a parameter to a method and delete it inside that method. This is inevitable when you need to use multiple threads. Well, best practices say that the owner (caller) should dispose of it.
eg.
public void MyMethod(MyClass reader){ using(reader){
What if the owner (thread creation) no longer exists? For instance.
interface IReader : IDisposable { string Read(); } public class MyReader : IReader { public string Read() { return "hellow world"; } public void Dispose() {
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); }
source share