2 using System
.Threading
;
3 using System
.Runtime
.ConstrainedExecution
;
10 public static int Count
= 0;
12 public P (int index
, ManualResetEvent mre
)
22 Console
.Write (String
.Format ("[{0}] Finalize\n", index
));
24 Console
.Write (String
.Format ("[{0}] Finalize -- end\n", index
));
34 int collected
, total
= 100;
36 for (int i
= 1; i
<= 1000; ++i
) {
39 mre
= new ManualResetEvent (false);
41 thread
= new Thread (() => {
42 for (int j
= 0; j
< total
; ++j
)
50 Console
.Write (String
.Format ("[{0}] Wait for pending finalizers\n", i
));
51 GC
.WaitForPendingFinalizers ();
52 Console
.Write (String
.Format ("[{0}] Wait for pending finalizers -- end\n", i
));
56 if (!mre
.WaitOne (5000)) {
57 Console
.Write (String
.Format ("[{0}] Finalizer never started\n", i
));
61 Console
.Write (String
.Format ("[{0}] Wait for pending finalizers (2)\n", i
));
62 GC
.WaitForPendingFinalizers ();
63 Console
.Write (String
.Format ("[{0}] Wait for pending finalizers (2) -- end\n", i
));
67 /* At least 1 finalizer started (as mre has been Set), but P.Count has not been incremented */
68 Console
.Write (String
.Format ("[{0}] Did not wait for finalizers to run\n", i
));
73 if (collected
!= total
) {
74 /* Not all finalizer finished, before returning from WaitForPendingFinalizers. Or not all objects
75 * have been garbage collected; this might be due to false pinning */
76 Console
.Write (String
.Format ("[{0}] Finalized {1} of {2} objects\n", i
, collected
, total
));