![](/img/trans.png)
[英]AppDomain.CurrentDomain.ProcessExit doesn't perform Console.WriteLine
[英]AppDomain.CurrentDomain.DomainUnload not be raised in Console app
我有一個程序集,當訪問時會旋轉一個線程來處理放在隊列中的項目。 在該程序集中,我將一個處理程序附加到DomainUnload事件:
AppDomain.CurrentDomain.DomainUnload += new EventHandler(CurrentDomain_DomainUnload);
該處理程序將線程連接到主線程,以便隊列中的所有項都可以在應用程序終止之前完成處理。
我遇到的問題是,當控制台應用程序終止時,DomainUnload事件不會被觸發。 任何想法為什么會這樣?
使用.NET 3.5和C#
您需要訂閱特定域的事件。 您也不能依賴域在終止時卸載。 從此代碼中刪除注釋以查看:
using System;
using System.Reflection;
class Program {
static void Main(string[] args) {
var ad = AppDomain.CreateDomain("test");
ad.DomainUnload += ad_DomainUnload;
//AppDomain.Unload(ad);
Console.ReadLine();
}
static void ad_DomainUnload(object sender, EventArgs e) {
Console.WriteLine("unloaded, press Enter");
Console.ReadLine();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.