Re: Garbage collection and async operations
- From: "Bob Altman" <rda@xxxxxxxxxxxxx>
- Date: Thu, 30 Nov 2006 15:25:20 -0800
So if I really don't care about the result of the asynchronous operation
(including any exception that it may have thrown) then nothing bad happens
if I don't call EndInvoke. (My actual application has a Cancel method on
the class containing the long-running async operation. But cancellation may
take a while. So after I call Cancel I just get rid of my reference to the
object and assume that it will eventually stop running and the GC will
eventually collect it.)
Thanks a million for helping me understand this!
- Bob
"Jon Shemitz" <jon@xxxxxxxxxxxxxxxxx> wrote in message
news:456F593E.ADF5837E@xxxxxxxxxxxxxxxxxxxx
Bob Altman wrote:
1. If I call a delegate's BeginInvoke method then the invoked routine
will
run to compltion on a thread pool thread, regardless of whether or not
that
routine or its delegate goes out of scope.
Yes.
2. When the background routine completes, its thread pool thread may or
may
not be assigned some other work, or that thread may be terminated. This
mechanism is independent of whether or not I call EndInvoke.
Yes.
3. If the Delegate goes out of scope before I call EndInvoke then I leak
the
Delegate (which presumably holds the return value from the invoked
routine
and the Exception object thrown by the invoked routine). Specifically,
some
magic in the CLR keeps the Delegate alive until its EndInvoke method is
called.
No. The return value or exception are stored in the object that
implements IAsyncResult. (Think about it: Many (most?) delegates are
never executed asynchronously. The IAsyncResult is explicitly created
by the runtime when you call BeginInvoke. The IAsyncResult must be
passed to EndInvoke. Where would *you* put the result?)
On a tangentially related subject: Is there a memory leak if I call a
Control object's BeginInvoke method (to run a routine on the UI thread)
and
never call the Control's EndInvoke method? Specifically, I use this code
pattern for handling events that may be raised on another thread:
No. This is explicitly documented as safe.
--
.NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
.
- Follow-Ups:
- Re: Garbage collection and async operations
- From: William Sullivan
- Re: Garbage collection and async operations
- References:
- Garbage collection and async operations
- From: Bob Altman
- Re: Garbage collection and async operations
- From: Jon Shemitz
- Re: Garbage collection and async operations
- From: Bob Altman
- Re: Garbage collection and async operations
- From: William Sullivan
- Re: Garbage collection and async operations
- From: Bob Altman
- Re: Garbage collection and async operations
- From: Jon Shemitz
- Garbage collection and async operations
- Prev by Date: Re: Can't get Assembly.GetCallingAssembly() to reflect the proper assembly
- Next by Date: Re: Can't get Assembly.GetCallingAssembly() to reflect the proper assembly
- Previous by thread: Re: Garbage collection and async operations
- Next by thread: Re: Garbage collection and async operations
- Index(es):
Relevant Pages
|