reference not removed revisited

  • Thread starter Thread starter Frank
  • Start date Start date
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 ;-)
 
[snip]
Willy.

PS. That's why I asked to post the whole code BTW
-) ----------------------------> You're right.

Willy,

the stathread is automatically inserted when u create a new console
application, so I never questioned it. Should have known better.

Thanks for your help.
Regards
Frank
 
Frank said:
[snip]
Willy.

PS. That's why I asked to post the whole code
TW -) ----------------------------> You're right.

Willy,

the stathread is automatically inserted when u create a new console
application, so I never questioned it. Should have known better.

Thanks for your help.
Regards
Frank

It should not be done automatically, note this has been corrected in
whidbey.
VB.NET has this STAThread attribute implicitely set by the compiler, so
there you MUST set MTAThread explicitely.

Willy.
 
Back
Top