为什么子域关闭程序异常?
为什么一个应用程序域中的异常会影响另一个应用程序域?
如何防止程序关闭?
using System;
using System.Reflection;
using System.Threading;
namespace domain
{
public class Worker : MarshalByRefObject
{
public static void NotMyCodeThreadProc()
{
throw new Exception();
}
public void NotMyCode()
{
var thread = new Thread(NotMyCodeThreadProc);
thread.Start();
thread.Join();
}
}
class Program
{
static void Main()
{
AppDomain ad = AppDomain.CreateDomain("New domain");
Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "domain.Worker");
try
{
remoteWorker.NotMyCode();
}
catch
{
}
Console.WriteLine("!");
Console.ReadLine();
}
}
}
Why exception in one application domain affect another application domain?
How do I prevent the closing of the program?
using System;
using System.Reflection;
using System.Threading;
namespace domain
{
public class Worker : MarshalByRefObject
{
public static void NotMyCodeThreadProc()
{
throw new Exception();
}
public void NotMyCode()
{
var thread = new Thread(NotMyCodeThreadProc);
thread.Start();
thread.Join();
}
}
class Program
{
static void Main()
{
AppDomain ad = AppDomain.CreateDomain("New domain");
Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "domain.Worker");
try
{
remoteWorker.NotMyCode();
}
catch
{
}
Console.WriteLine("!");
Console.ReadLine();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 .NET 2.0(及更高版本)中,线程中未处理的异常会导致整个进程终止。
您可以按照 Hans 的建议更改该策略,或者您可以简单地用 try/catch 包装代码并处理异常。
In .NET 2.0 (and above), an unhandled exception in a thread causes the entire process to terminate.
You can change that policy by following Hans' advice, or you can simply wrap your code with try/catch and handle the exception.