2 using System
.Threading
;
3 using System
.Reflection
;
9 Console
.WriteLine ("FINALIZING IN DOMAIN " + AppDomain
.CurrentDomain
.FriendlyName
+ ": " + AppDomain
.CurrentDomain
.IsFinalizingForUnload ());
14 public class SlowFinalize
{
17 Console
.WriteLine ("FINALIZE1.");
21 catch (Exception ex
) {
22 Console
.WriteLine ("A: " + ex
);
24 Console
.WriteLine ("FINALIZE2.");
29 public class AThread
{
32 new Thread (new ThreadStart (Run
)).Start ();
40 catch (ThreadAbortException ex
) {
41 Console
.WriteLine ("Thread aborted correctly.");
46 // A Thread which refuses to die
47 public class BThread
: MarshalByRefObject
{
52 new Thread (new ThreadStart (Run
)).Start ();
64 catch (ThreadAbortException ex
) {
71 public class UnloadThread
{
75 public UnloadThread (AppDomain domain
) {
80 Console
.WriteLine ("UNLOAD1");
81 AppDomain
.Unload (domain
);
82 Console
.WriteLine ("UNLOAD2");
86 class CrossDomainTester
: MarshalByRefObject
92 public static int Main() {
93 return TestDriver
.RunTests (typeof (Tests
));
96 public static int test_0_unload () {
97 for (int i
= 0; i
< 10; ++i
) {
98 AppDomain appDomain
= AppDomain
.CreateDomain("Test-unload" + i
);
100 appDomain
.CreateInstanceAndUnwrap (
101 typeof (CrossDomainTester
).Assembly
.FullName
, "CrossDomainTester");
103 AppDomain
.Unload(appDomain
);
109 public static int test_0_unload_default () {
111 AppDomain
.Unload (Thread
.GetDomain ());
113 catch (CannotUnloadAppDomainException
) {
119 public static int test_0_unload_after_unload () {
120 AppDomain domain
= AppDomain
.CreateDomain ("Test2");
121 AppDomain
.Unload (domain
);
124 AppDomain
.Unload (domain
);
133 public static int test_0_is_finalizing () {
134 AppDomain domain
= AppDomain
.CreateDomain ("Test-is-finalizing");
135 object o
= domain
.CreateInstanceFromAndUnwrap (typeof (Tests
).Assembly
.Location
, "Foo");
137 if (domain
.IsFinalizingForUnload ())
140 AppDomain
.Unload (domain
);
145 public static int test_0_unload_with_active_threads () {
146 AppDomain domain
= AppDomain
.CreateDomain ("Test3");
147 object o
= domain
.CreateInstanceFromAndUnwrap (typeof (Tests
).Assembly
.Location
, "AThread");
150 AppDomain
.Unload (domain
);
155 public static int test_0_unload_with_active_threads_timeout () {
156 AppDomain domain
= AppDomain
.CreateDomain ("Test4");
157 BThread o
= (BThread
)domain
.CreateInstanceFromAndUnwrap (typeof (Tests
).Assembly
.Location
, "BThread");
161 AppDomain
.Unload (domain
);
166 AppDomain
.Unload (domain
);
173 static void Worker (object x
) {
174 Thread
.Sleep (100000);
177 public static void invoke_workers () {
178 for (int i
= 0; i
< 1; i
++)
179 ThreadPool
.QueueUserWorkItem (Worker
);
182 public static int test_0_unload_with_threadpool () {
183 AppDomain domain
= AppDomain
.CreateDomain ("test_0_unload_with_threadpool");
185 domain
.DoCallBack (new CrossAppDomainDelegate (invoke_workers
));
186 AppDomain
.Unload (domain
);
192 * This test is not very deterministic since the thread which enqueues
193 * the work item might or might not be inside the domain when the unload
194 * happens. So disable this for now.
197 public static void DoUnload (object state) {
198 AppDomain.Unload (AppDomain.CurrentDomain);
201 public static void Callback () {
202 Console.WriteLine (AppDomain.CurrentDomain);
203 WaitCallback unloadDomainCallback = new WaitCallback (DoUnload);
204 ThreadPool.QueueUserWorkItem (unloadDomainCallback);
207 public static int test_0_unload_inside_appdomain_async () {
208 AppDomain domain = AppDomain.CreateDomain ("Test3");
210 domain.DoCallBack (new CrossAppDomainDelegate (Callback));
216 public static void SyncCallback () {
217 AppDomain
.Unload (AppDomain
.CurrentDomain
);
220 public static int test_0_unload_inside_appdomain_sync () {
221 AppDomain domain
= AppDomain
.CreateDomain ("Test3");
224 domain
.DoCallBack (new CrossAppDomainDelegate (SyncCallback
));
226 catch (Exception ex
) {
227 /* Should throw a ThreadAbortException */
228 Thread
.ResetAbort ();
234 // FIXME: This does not work yet, because the thread is finalized too
237 public static int test_0_unload_during_unload () {
238 AppDomain domain = AppDomain.CreateDomain ("Test3");
239 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "SlowFinalize");
241 UnloadThread t = new UnloadThread (domain);
243 // Start unloading in a separate thread
244 new Thread (new ThreadStart (t.Run)).Start ();
249 AppDomain.Unload (domain);
252 Console.WriteLine ("OK");