W
Willy Denoyette [MVP]
Frank said:Willy,
at last!!!!!!!!!!!! I found it. I use [STAThread] in my main class.
Removing that leaves no live instances to weakreferences and adding it
gives lots of references.
Again, thanks for your help.
But, why does this line have this reaction?
Regards
Frank
Frank,
You should NEVER apply the STAThread attribute to the main entry of a
console application unless you pump the message queue.
f.i by calling DoEvents like in the sample I posted:
using System.Windows.Forms;
....
if (i % 1000 == 0)
{
Application.DoEvents();
Console.WriteLine(i);
}
The reason for this is that a thread that runs in a STA (Single Threaded
Apartment) can only be called by the Finalizer thread through
inter-apartment marshaling, this marshaling mechanism uses the windows
message queue, and requires the callee to pump the message queue (something
that gets done automatically in a Windows Forms application).
Failing to pump messages in the STA thread effectively blocks the Finalizer
to run, that means finalizable objects in the STA don't get released and
this is what happens here with the WeakReference instances.
Willy.
PS. That's why I asked to post the whole code BTW ;-)