2 using System.Threading;
8 Console.WriteLine ("FINALIZING IN DOMAIN " + AppDomain.CurrentDomain.FriendlyName + ": " + AppDomain.CurrentDomain.IsFinalizingForUnload ());
13 public class SlowFinalize {
16 Console.WriteLine ("FINALIZE1.");
20 catch (Exception ex) {
21 Console.WriteLine ("A: " + ex);
23 Console.WriteLine ("FINALIZE2.");
28 public class AThread {
31 new Thread (new ThreadStart (Run)).Start ();
39 catch (ThreadAbortException ex) {
40 Console.WriteLine ("Thread aborted correctly.");
45 // A Thread which refuses to die
46 public class BThread : MarshalByRefObject {
51 new Thread (new ThreadStart (Run)).Start ();
63 catch (ThreadAbortException ex) {
70 public class UnloadThread {
74 public UnloadThread (AppDomain domain) {
79 Console.WriteLine ("UNLOAD1");
80 AppDomain.Unload (domain);
81 Console.WriteLine ("UNLOAD2");
87 public static int Main() {
88 return TestDriver.RunTests (typeof (Tests));
91 public static int test_0_unload () {
92 for (int i = 0; i < 10; ++i) {
93 AppDomain appDomain = AppDomain.CreateDomain("Test-unload" + i);
94 AppDomain.Unload(appDomain);
100 public static int test_0_unload_default () {
102 AppDomain.Unload (Thread.GetDomain ());
104 catch (CannotUnloadAppDomainException) {
110 public static int test_0_unload_after_unload () {
111 AppDomain domain = AppDomain.CreateDomain ("Test2");
112 AppDomain.Unload (domain);
115 AppDomain.Unload (domain);
124 public static int test_0_is_finalizing () {
125 AppDomain domain = AppDomain.CreateDomain ("Test-is-finalizing");
126 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "Foo");
128 if (domain.IsFinalizingForUnload ())
131 AppDomain.Unload (domain);
136 public static int test_0_unload_with_active_threads () {
137 AppDomain domain = AppDomain.CreateDomain ("Test3");
138 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "AThread");
141 AppDomain.Unload (domain);
146 public static int test_0_unload_with_active_threads_timeout () {
147 AppDomain domain = AppDomain.CreateDomain ("Test4");
148 BThread o = (BThread)domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "BThread");
152 AppDomain.Unload (domain);
157 AppDomain.Unload (domain);
165 * This test is not very deterministic since the thread which enqueues
166 * the work item might or might not be inside the domain when the unload
167 * happens. So disable this for now.
170 public static void DoUnload (object state) {
171 AppDomain.Unload (AppDomain.CurrentDomain);
174 public static void Callback () {
175 Console.WriteLine (AppDomain.CurrentDomain);
176 WaitCallback unloadDomainCallback = new WaitCallback (DoUnload);
177 ThreadPool.QueueUserWorkItem (unloadDomainCallback);
180 public static int test_0_unload_inside_appdomain_async () {
181 AppDomain domain = AppDomain.CreateDomain ("Test3");
183 domain.DoCallBack (new CrossAppDomainDelegate (Callback));
189 public static void SyncCallback () {
190 AppDomain.Unload (AppDomain.CurrentDomain);
193 public static int test_0_unload_inside_appdomain_sync () {
194 AppDomain domain = AppDomain.CreateDomain ("Test3");
197 domain.DoCallBack (new CrossAppDomainDelegate (SyncCallback));
199 catch (Exception ex) {
200 /* Should throw a ThreadAbortException */
201 Thread.ResetAbort ();
207 // FIXME: This does not work yet, because the thread is finalized too
210 public static int test_0_unload_during_unload () {
211 AppDomain domain = AppDomain.CreateDomain ("Test3");
212 object o = domain.CreateInstanceFromAndUnwrap (typeof (Tests).Assembly.Location, "SlowFinalize");
214 UnloadThread t = new UnloadThread (domain);
216 // Start unloading in a separate thread
217 new Thread (new ThreadStart (t.Run)).Start ();
222 AppDomain.Unload (domain);
225 Console.WriteLine ("OK");