一个应用域中的错误会影响另一个应用域的代码 [英] Fault in one app domain affecting code of another app domain

查看:82
本文介绍了一个应用域中的错误会影响另一个应用域的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据msdn的文档一个应用程序域中的错误不会影响在另一个应用程序域中运行的其他代码。



主程序:



As per documentatio at msdn "Faults in one application domain cannot affect other code running in another application domain.

Main Program:

System.AppDomain myDomain = System.AppDomain.CreateDomain("MyDomain");

             myDomain.ExecuteAssembly(@"C:\Users\delllaptop\Documents\Visual Studio 2012\Projects\AppDomain\HelloWorld\bin\Debug\HelloWorld.exe");

< br $>


HelloWorld代码:





HelloWorld Code:

string abc = "abc";
                int i = Convert.ToInt16(abc);





但在我的情况下,当我跑主程序和加载和执行helloworld.exe(生成异常),正在影响主程序崩溃。



But in my case, when i run main program and loading and executing helloworld.exe (which generating exception), is affecting main program to crash.

推荐答案

首先,不能影响在其他应用程序域中运行的其他代码是一个某种夸张或一种比喻。它只是意味着它们的地址空间是严格隔离的,就像单独的进程一样。你不应该从字面上理解它。应用程序域可以通过IPC进行通信,还有专门为Application Domain定义的其他简化的类似IPC的方法。一个例子是 System.AppDomain.SetData / GetData 。我的观点是:例如,如果应用程序域交换消息,并且一条消息显示自杀,并且如果某些应用程序域被编程为响应此类消息而自行终止,您是否会将其称为影响? :-)



你的情况有所不同。首先,您应该注意以下事实:您的 myDomain.ExecuteAssembly 方法不会在您称为MyDomain的域中执行。它在创建该新域的域中执行。此调用的副作用是在MyDomain域中执行HelloWord.exe。



现在,您应该了解这些域只是隔离的在地址空间。代码和调用堆栈是共享的。特别是,这意味着如果在一个应用程序域中抛出一些异常,它将传播到另一个异常,其代码位于堆栈上方。这就是发生的事情。这非常好,不错。



如果你有一个单独的线程,它会有所不同:异常会传播到顶层线程但不影响父线程。您可以自由组合线程和应用程序域:在一个应用程序域中执行一个或多个线程,或在不同的应用程序域中执行相同的线程。
与线程相关,请注意:
First of all, "cannot affect other code running in another application domain" is a certain exaggeration, or a figure of speech. It simply means that their address spaces are strictly isolated, like in the case of separate processes. You should not understand it literally. Application Domains can communicate via IPC, and there are additional simplified IPC-like methods defined specially for Application Domain. One example is System.AppDomain.SetData/GetData. My point is: if, for example, Application Domains exchange messages, and one message says "kill yourself", and if some Application Domain is programmed to kill itself in response to such message, will you call it "affecting"? :-)

Your case is different though. First of all, you should pay attention for the fact: your myDomain.ExecuteAssembly method is executed not in the domain you called "MyDomain". It is executed in the domain which created that new domain. And the side effect of this call is the execution of "HelloWord.exe" in the domain "MyDomain".

Now, you should understand that the domains are only isolated in address spaces. The code and call stack are shared. In particular, it means that if some exception is thrown in one Application Domain, it will be propagated to another one with the code upper on stack. This is what happens. And this is very good, not bad.

If you had a separate thread, it would be different: the exception would propagate to the top of the thread but not affect the parent thread. You can freely combine threads and Application Domains: execute one or more threads in one Application Domain, or same thread in different Application Domains.
In connection to threading, pay attention:
MSDN 告诉我们:
MSDN tells us:

此方法不会创建新的进程或应用程序域,也不会在新线程上执行入口点方法。

This method does not create a new process or application domain, and it does not execute the entry point method on a new thread.

我可以告诉你它的确实做了什么:它将所述程序集加载到所述Domain的相同进程,试图找到入口点方法(通常是 Main 方法),如果找到,只需在当前线程中调用它,同一个线程作为调用 AppDomain.ExecuteAssembly 的人。这样,应用程序程序集就不会真正用作应用程序。使用.NET,这是正常的:EXE和DLL之间没有根本区别,在这种情况下,您的EXE用作插件DLL。



最后,你不应该称之为崩溃。对于软件开发人员来说,没有崩溃之类的东西,但是有一些例外,特别是那些被遗忘的东西,直到每个主题的顶部。



-SA

I can tell you what it really does: it loads the said assembly in the same process of the said Domain, tries to find the entry point method (normally, the Main method) and, if found, just calls it in the current threads, the same thread as the one calling AppDomain.ExecuteAssembly. This way, the application assembly is not really used as the application. With .NET, this is normal: there is no a fundamental difference between "EXE" and "DLL", in this case, your "EXE" is used as a kind of a plug-in "DLL".

And finally, you should not call this a "crash". For software developers, there is no such thing as "crash", but there are exception, in particular, those forgotten to be ever caught, up to the top of each thread.

—SA


这篇关于一个应用域中的错误会影响另一个应用域的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆