> I just read source code witch use WeakReference object to implements some
> Cache system.
[quoted text clipped - 10 lines]
> I don't know if it is the main thread to do sometime GC.Collect() or
> another thread ?
More likely is that another thread will cause a garbage collection
implicitly. I'm not entirely sure what your question is here, but any
thread can trigger a garbage collection.
> If it is another Thread, I think we need a critical section between "Point
> A" and "Point C"
>
> moreover, It is possible we use this technic with Compact Framework
> application. >> do you need to be careful about something else ?
I suggest just using WeakReference.Target and testing the result of
nullity after retrieving it. That way you've got the reference before
you do the test, so if it's alive, you don't need to worry about it
then being GC'd.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Yes, there is a potential race condition here.
You can easily get around it with:
MyClass myObj = (MyClass)myWeakReference.Target;
if (myObj != null)
myObj.DoSomething();
Bruno
> Hello !
>
[quoted text clipped - 20 lines]
>
> Thanks for your help !
Thibaud Bouquely - 29 Dec 2004 18:06 GMT
Thanks for yours responses
We could say "MyWeakReference.IsAlive" is not very useful, no ?
the only "good" way to know is a target is alive is to use your technic ...
>> with Reflector, In WeakReference.IsAlive, we can read this
...
return (GCHandle.InternalGet(num1) != null);
and in WeakReference.Get_Target we can read too :
object obj1 = GCHandle.InternalGet(num1);
return obj1;
>> it is another fact to confirm your solution ...
> Yes, there is a potential race condition here.
>
[quoted text clipped - 30 lines]
> >
> > Thanks for your help !
Bruno Jouhier [MVP] - 29 Dec 2004 18:53 GMT
> Thanks for yours responses
>
> We could say "MyWeakReference.IsAlive" is not very useful, no ?
Agreed! I have used weak references a number of times, and I never used this
call.
Bruno.