|
Please someone correct me if I am wrong,
What I believe you created is a finalizer. Dispose is implemented as
a method you call on the object to clean up resources. The finalizer is not
called until the object is actually garbage collected. So it could sit in memory
for an undetermined amount of time before the finalizer is called. (after
writing this and reading two more times, Gonzalo does have a point)
The IDispose interface is used to be able to clear up resources without
necessarily being garbage collected right away.
Here are some articles on MSDN:
This article is particularly good...
Now I may be off base at what's being asked, but In general if you follow
the linked articles, it generally should work.
Richard Norman
Web/Application Developer
El dom, 06-07-2003 a las 17:20, Giuseppe Greco escribi=:
> Hi all, > > I've a class that starts a worker thread like > this: > > class MyClass > { > bool isDisposed = false; > Thread myThread; > > public MyClass() > { > ... > myThread = new Thread(new ThreadStart(MyThreadMethod)); > myThread.Start(); > myThread.IsBackground = true; > } > > ~MyClass() > { > Dispose(false) > } > > public void Dispose() > { > Dispose(true); > GC.SuppressFinalize(this); > } > > protected virtual void Dispose(bool disposing) > { > if (!isDisposed) { > isDisposed = true; > > myThread.Join(); // should wait until MyThreadMethod() > // completes its work... > myThread = null; > > if (disposing) { > ... > } > } > } > > private void MyThreadMethod() > { > while (!isDisposed) { > ... > { > } > } > > Well, in the class above, the Dispose() method is never > called. This is a problem if one needs to wait until the > thread has finished its work -- Thread.Join() should block > until then. > > The destructor -- ~MyClass() -- is never called. Is it called under windows? I think it's not because MyThreadMethod
is
accessing isDisposed field, which belongs to the class instance. That's why it's never disposed. -Gonzalo
|
- [Mono-list] Dispose method is never called Giuseppe Greco
- Re: [Mono-list] Dispose method is never calle... James F. Bellinger
- Re: [Mono-list] Dispose method is never c... Giuseppe Greco
- Re: [Mono-list] Dispose method is never c... Giuseppe Greco
- Re: [Mono-list] Dispose method is never calle... Gonzalo Paniagua Javier
- Re: [Mono-list] Dispose method is never calle... Richard Norman
- Re: [Mono-list] Dispose method is never calle... Giuseppe Greco
- Re: [Mono-list] Dispose method is never c... Giuseppe Greco
- Re: [Mono-list] Dispose method is nev... Giuseppe Greco
- Re: [Mono-list] Dispose method is... Gonzalo Paniagua Javier
- Re: [Mono-list] Dispose meth... Gonzalo Paniagua Javier
- Re: [Mono-list] Dispose ... Giuseppe Greco
- Re: [Mono-list] Dispose method is never calle... Richard Norman
- Re: [Mono-list] Dispose method is never calle... Giuseppe Greco
- Re: [Mono-list] Dispose method is never c... Gonzalo Paniagua Javier
- Re: [Mono-list] Dispose method is nev... Giuseppe Greco
