Export (0) Print
Expand All

CannotUnloadAppDomainException Class

The exception that is thrown when an attempt to unload an application domain fails.

For a list of all members of this type, see CannotUnloadAppDomainException Members.

System.Object
   System.Exception
      System.SystemException
         System.CannotUnloadAppDomainException

[Visual Basic]
<Serializable>
Public Class CannotUnloadAppDomainException
   Inherits SystemException
[C#]
[Serializable]
public class CannotUnloadAppDomainException : SystemException
[C++]
[Serializable]
public __gc class CannotUnloadAppDomainException : public
   SystemException
[JScript]
public
   Serializable
class CannotUnloadAppDomainException extends SystemException

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

CannotUnloadAppDomainException is thrown when there is an attempt to unload:

  • The default application domain, which must remains loaded during the lifetime of the application.
  • An application domain with a running thread that cannot immediately stop execution.
  • An application domain that has already been unloaded.

CannotUnloadAppDomainException uses the HRESULT COR_E_CANNOTUNLOADAPPDOMAIN, which has the value 0x80131015.

For a list of initial property values for an instance of CannotUnloadAppDomainException, see the CannotUnloadAppDomainException constructors.

Example

[Visual Basic] 
Imports System
Imports System.Reflection
Imports System.Security.Policy
 'for evidence object

Class ADMultiDomain
   
   ' The following attribute indicates to loader that multiple application 
   ' domains are used in this application.
   <LoaderOptimizationAttribute(LoaderOptimization.MultiDomainHost)>  _
   Public Shared Sub Main()
      ' Create application domain setup information for new application domain.
      Dim domaininfo As New AppDomainSetup()
      domaininfo.ApplicationBase = "file:///" + System.Environment.CurrentDirectory
      domaininfo.ApplicationName = "MyMultiDomain Application"
      
      'Create evidence for the new appdomain from evidence of current application domain.
      Dim adevidence As Evidence = AppDomain.CurrentDomain.Evidence
      
      ' Create appdomain.
      Dim newDomain As AppDomain = AppDomain.CreateDomain("MyMultiDomain", adevidence, domaininfo)
      
      'Load an assembly into new the application domain.
      newDomain.Load("System.data, version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
      
      'Unload the application domain, which also unloads the assembly.
      AppDomain.Unload(newDomain)
      
      Try
         AppDomain.Unload(newDomain)
      Catch e As CannotUnloadAppDomainException
         Console.WriteLine("System can't unload appdomain.  " & e.ToString())
      End Try
   End Sub 'Main
End Class 'ADMultiDomain

[C#] 
using System;
using System.Reflection;
using System.Security.Policy;  //for evidence object

class ADMultiDomain
{
    // The following attribute indicates to loader that multiple application 
    // domains are used in this application.
    [LoaderOptimizationAttribute( LoaderOptimization.MultiDomainHost)]
    public static void Main()
    {
        // Create application domain setup information for new application domain.
        AppDomainSetup domaininfo = new AppDomainSetup();
        domaininfo.ApplicationBase = "file:///" + System.Environment.CurrentDirectory;
        domaininfo.ApplicationName = "MyMultiDomain Application";

        //Create evidence for the new appdomain from evidence of current application domain.
        Evidence adevidence = AppDomain.CurrentDomain.Evidence;

        // Create appdomain.
        AppDomain newDomain = AppDomain.CreateDomain("MyMultiDomain", adevidence, domaininfo);

        //Load an assembly into new the application domain.
        newDomain.Load("System.data, version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

        //Unload the application domain, which also unloads the assembly.
        AppDomain.Unload(newDomain);

        try
        {    
        AppDomain.Unload(newDomain);
        }
        catch (CannotUnloadAppDomainException e)
            {
             Console.WriteLine("System can't unload appdomain.  " + e);
            }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Security::Policy;  //for evidence Object*

// The following attribute indicates to loader that multiple application
// domains are used in this application.
[LoaderOptimizationAttribute(LoaderOptimization::MultiDomainHost)]
int main() {
   // Create application domain setup information for new application domain.
   AppDomainSetup* domaininfo = new AppDomainSetup();
   domaininfo->ApplicationBase = S"file:////{0}", System::Environment::CurrentDirectory;
   domaininfo->ApplicationName = S"MyMultiDomain Application";

   //Create evidence for the new appdomain from evidence of current application domain.
   Evidence* adevidence = AppDomain::CurrentDomain->Evidence;

   // Create appdomain.
   AppDomain*  newDomain = AppDomain::CreateDomain(S"MyMultiDomain", adevidence, domaininfo);

   //Load an assembly into new the application domain.
   newDomain->Load(S"System.data, version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

   //Unload the application domain, which also unloads the assembly.
   AppDomain::Unload(newDomain);

   try {
      AppDomain::Unload(newDomain);
   } catch (CannotUnloadAppDomainException* e) {
      Console::WriteLine(S"System can't unload appdomain. {0}", e);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

CannotUnloadAppDomainException Members | System Namespace | Object | Exception | AppDomain | Thread | Handling and Throwing Exceptions

Show:
© 2014 Microsoft