Re: Relationship between Application.Exit() and AppDomain
- From: Sunny S <sunny.s@xxxxxxxxxxxxxxxx>
- Date: Tue, 15 Apr 2008 11:21:03 -0700
"Willy Denoyette [MVP]" wrote:
I agree with Jeffrey here, AppDomain.Unload() can/should not be blocked by a
running/blocked UI thread, in those cases where Thread.Abort did not
terminate the thread, the CLR would escalate to a rude abort which cannot be
blocked anyway.
Willy.
Hi Willy,
Thanks for replying. I actually wasn’t talking about a UI thread, if I
understood your comment correctly. To avoid confusion, I’ve appended the code
that illustrates my point.
The code creates two forms, UI1 and UI2. Hit the ‘Start Thread’ button on
UI2 and then the ‘Unload AD2’ button on UI1. It doesn’t work every time on my
box, but in about half of all attempts I get the
CannotUnloadAppDomainException. To increase your chances:) you may have to
hit the ‘Start Thread’ several times to create more threads, especially if
you run this app outside VS.
Regards,
SS
... and here is the code:
using System;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
namespace AppDomains
{
public class Test : MarshalByRefObject
{
static void Main() {
Test t1 = new Test();
t1.StartUI1();
}
public void StartUI1() {
Thread t = new Thread(ShowUI1);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void ShowUI1() {
AppDomain ad2 = AppDomain.CreateDomain("AD2");
// You might need to change the type name here...
Test t2 =
(Test)ad2.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName,
"AppDomains.Test");
t2.StartUI2();
Form form = new Form {
Size = new Size(200, 200),
Text = "Form UI1"
};
Button b = new Button {
Text = "Unload AD2",
Location = new Point(5, 50)
};
b.Click += (s, e) => { AppDomain.Unload(ad2); };
form.Controls.Add(b);
Application.Run(form);
}
public void StartUI2() {
Thread t = new Thread(ShowUI2);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void ShowUI2() {
Form form = new Form {
Size = new Size(200, 200),
Text = "Form UI2"
};
Button b = new Button {
Text = "Start Thread",
Location = new Point(5, 50)
};
b.Click += (s, e) => {
Thread t = new Thread(T);
t.Start();
};
form.Controls.Add(b);
Application.Run(form);
}
private void T() {
while (true) { };
}
}
}
.
- Follow-Ups:
- Re: Relationship between Application.Exit() and AppDomain
- From: "Jeffrey Tan[MSFT]"
- Re: Relationship between Application.Exit() and AppDomain
- References:
- Relationship between Application.Exit() and AppDomain
- From: Sunny S
- Re: Relationship between Application.Exit() and AppDomain
- From: Scott M.
- Re: Relationship between Application.Exit() and AppDomain
- From: Jon Skeet [C# MVP]
- Re: Relationship between Application.Exit() and AppDomain
- From: Sunny S
- Re: Relationship between Application.Exit() and AppDomain
- From: Willy Denoyette [MVP]
- Re: Relationship between Application.Exit() and AppDomain
- From: Sunny S
- Re: Relationship between Application.Exit() and AppDomain
- From: Willy Denoyette [MVP]
- Re: Relationship between Application.Exit() and AppDomain
- From: Sunny S
- Re: Relationship between Application.Exit() and AppDomain
- From: Willy Denoyette [MVP]
- Re: Relationship between Application.Exit() and AppDomain
- From: Sunny S
- Re: Relationship between Application.Exit() and AppDomain
- From: Jeffrey Tan[MSFT]
- Re: Relationship between Application.Exit() and AppDomain
- From: Sunny S
- Re: Relationship between Application.Exit() and AppDomain
- From: "Jeffrey Tan[MSFT]"
- Re: Relationship between Application.Exit() and AppDomain
- From: Sunny S
- Re: Relationship between Application.Exit() and AppDomain
- From: Willy Denoyette [MVP]
- Relationship between Application.Exit() and AppDomain
- Prev by Date: Re: Relationship between Application.Exit() and AppDomain
- Next by Date: publisher policy assemblies
- Previous by thread: Re: Relationship between Application.Exit() and AppDomain
- Next by thread: Re: Relationship between Application.Exit() and AppDomain
- Index(es):