none
Appdomain and thread RRS feed

  • Question

  • I'm using Appdomain and threading as given below in my application. I have checked current Thread and Appdomain Names in the FileWriter Method. If I=1 the appdomain and the thread Names are 1

    My question is if unload the appdomain from the FileWriter Method will that release the thread and the class objects. My sample application is working but I'm not sure if its releasing the resources.

    Example Code:

    1. foreach(int i in idlist)
    2. {
    3.  string appDNname = WSMonitor + i.ToString();
    4.  AppDomain WMSObj = AppDomain.CreateDomain(appDNname);
    5.  Type t= typeof(Monitoring);
    6.  Monitoring monWSObj = (Monitoring) WMSObj.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, t.FullName);
    7. Thread WorkerT = new Thread(monWSObj.FileWriter);
    8. WorkerT.Start(i);
    9. }
    Tuesday, August 10, 2010 11:35 AM

Answers

  • You instantiate the thread in the current appdomain. No reason for it to be released when some other appdomain is unloaded.

    Edit:

    While the Thread object has no connection with the AppDomain, the method you're executing is on an object which will be released when the appdomain is unloaded. The method (and the thread executing it) will end abruptly when the appdomain unloads. A BackgroundWorker would report an AppDomainUnloadedException.

    • Marked as answer by SamAgain Thursday, August 19, 2010 9:14 AM
    Tuesday, August 10, 2010 9:45 PM
  • Hi,

    Thanks for your post.

    If I understand it right. You want to create an object in another AppDomain and execute its method on a thread. In this method, you want to unload the AppDomain.

    The result depends on what the object's type looks like, i.e. the Monitoring type. In order to create its object in another AppDomain, it must be serializable, or derive from MarshalByRefObject. But either way exceptions will be thrown. Though different exceptions they are.

    MarshalByRefObject: Thread has aborted. (Exception from HRESULT: 0x80131530)

    Serializable:  Error while unloading appdomain. (Exception from HRESULT: 0x80131015)

    Here is a complete sample code:

    using System;
    using System.Reflection;
    using System.Runtime.Remoting;
    using System.Threading;
    
    namespace UnloadADFromAThreadRunningMethodInIt
    {
     class Program
     {
      static void Main(string[] args)
      {
       AppDomain ad2 = AppDomain.CreateDomain("AD2");
       TypeInOtherAD proxy = ad2.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName,
        "UnloadADFromAThreadRunningMethodInIt.TypeInOtherAD") as TypeInOtherAD;
    
       Console.WriteLine("is proxy? {0}", RemotingServices.IsTransparentProxy(proxy));
       Thread worker = new Thread(proxy.method);
       worker.IsBackground = true;
       worker.Start();
       worker.Join();
      }
    
      static void show_all_the_assemblies_in_ad(AppDomain ad)
      {
       Console.WriteLine();
       Console.WriteLine("---------AppDomain: {0}---------", ad.FriendlyName);
       Assembly[] assemblies = ad.GetAssemblies();
       int index = 1;
       foreach (Assembly asm in assemblies)
       {
        Console.WriteLine("#{0}.{1}", index++, asm.FullName);
       }
       Console.WriteLine();
      }
    
     }
    
     //[Serializable]
     public class TypeInOtherAD : MarshalByRefObject
     {
      public void method()
      {
       AppDomain current_domain = AppDomain.CurrentDomain;
       Console.WriteLine("In AD:{0}", current_domain.FriendlyName);
       show_all_the_assemblies_in_ad(current_domain);
       try
       {
        AppDomain.Unload(current_domain);
       }
       catch (Exception e)
       {
        Console.WriteLine(e.Message);//Thread abort exception
       }
      }
    
      static void show_all_the_assemblies_in_ad(AppDomain ad)
      {
       Console.WriteLine();
       Console.WriteLine("---------AppDomain: {0}---------", ad.FriendlyName);
       Assembly[] assemblies = ad.GetAssemblies();
       int index = 1;
       foreach (Assembly asm in assemblies)
       {
        Console.WriteLine("#{0}.{1}", index++, asm.FullName);
       }
       Console.WriteLine();
      }
    
     }
    }
    
    

    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Wednesday, August 11, 2010 5:25 AM refine
    • Marked as answer by SamAgain Thursday, August 19, 2010 9:13 AM
    Wednesday, August 11, 2010 5:24 AM

All replies

  • I have a class Monitoring and I'm loading this in to appdomain and also creating a new thread and invoking Method FileWriter. I checked domain Name and thread name in FileWriter Method for the each new creation of domain and Thread are same. i.e. If i=1 thread name is 1 and appdomain is 1. I'm calling the unload appdomain in the FileWriter Method i.e. AppDomain.Unload(AppDomain.Current);

    My question:

    If I unload the the appdomain will the respective thread also be released?

    This is the sample code:

    1. foreach(int i in idlist)
    2. {
    3.  string appDNname = WSMonitor + i.ToString();
    4.  AppDomain WMSObj = AppDomain.CreateDomain(appDNname);
    5.  Type t= typeof(Monitoring);
    6.  Monitoring monWSObj = (Monitoring) WMSObj.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, t.FullName);
    7. Thread WorkerT = new Thread(monWSObj.FileWriter);
    8. WorkerT.Start(i);
    9. }
    • Merged by Figo Fei Monday, August 16, 2010 2:53 AM
    Tuesday, August 10, 2010 11:22 AM
  • You instantiate the thread in the current appdomain. No reason for it to be released when some other appdomain is unloaded.

    Edit:

    While the Thread object has no connection with the AppDomain, the method you're executing is on an object which will be released when the appdomain is unloaded. The method (and the thread executing it) will end abruptly when the appdomain unloads. A BackgroundWorker would report an AppDomainUnloadedException.

    • Marked as answer by SamAgain Thursday, August 19, 2010 9:14 AM
    Tuesday, August 10, 2010 9:45 PM
  • Hi,

    Thanks for your post.

    If I understand it right. You want to create an object in another AppDomain and execute its method on a thread. In this method, you want to unload the AppDomain.

    The result depends on what the object's type looks like, i.e. the Monitoring type. In order to create its object in another AppDomain, it must be serializable, or derive from MarshalByRefObject. But either way exceptions will be thrown. Though different exceptions they are.

    MarshalByRefObject: Thread has aborted. (Exception from HRESULT: 0x80131530)

    Serializable:  Error while unloading appdomain. (Exception from HRESULT: 0x80131015)

    Here is a complete sample code:

    using System;
    using System.Reflection;
    using System.Runtime.Remoting;
    using System.Threading;
    
    namespace UnloadADFromAThreadRunningMethodInIt
    {
     class Program
     {
      static void Main(string[] args)
      {
       AppDomain ad2 = AppDomain.CreateDomain("AD2");
       TypeInOtherAD proxy = ad2.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName,
        "UnloadADFromAThreadRunningMethodInIt.TypeInOtherAD") as TypeInOtherAD;
    
       Console.WriteLine("is proxy? {0}", RemotingServices.IsTransparentProxy(proxy));
       Thread worker = new Thread(proxy.method);
       worker.IsBackground = true;
       worker.Start();
       worker.Join();
      }
    
      static void show_all_the_assemblies_in_ad(AppDomain ad)
      {
       Console.WriteLine();
       Console.WriteLine("---------AppDomain: {0}---------", ad.FriendlyName);
       Assembly[] assemblies = ad.GetAssemblies();
       int index = 1;
       foreach (Assembly asm in assemblies)
       {
        Console.WriteLine("#{0}.{1}", index++, asm.FullName);
       }
       Console.WriteLine();
      }
    
     }
    
     //[Serializable]
     public class TypeInOtherAD : MarshalByRefObject
     {
      public void method()
      {
       AppDomain current_domain = AppDomain.CurrentDomain;
       Console.WriteLine("In AD:{0}", current_domain.FriendlyName);
       show_all_the_assemblies_in_ad(current_domain);
       try
       {
        AppDomain.Unload(current_domain);
       }
       catch (Exception e)
       {
        Console.WriteLine(e.Message);//Thread abort exception
       }
      }
    
      static void show_all_the_assemblies_in_ad(AppDomain ad)
      {
       Console.WriteLine();
       Console.WriteLine("---------AppDomain: {0}---------", ad.FriendlyName);
       Assembly[] assemblies = ad.GetAssemblies();
       int index = 1;
       foreach (Assembly asm in assemblies)
       {
        Console.WriteLine("#{0}.{1}", index++, asm.FullName);
       }
       Console.WriteLine();
      }
    
     }
    }
    
    

    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Wednesday, August 11, 2010 5:25 AM refine
    • Marked as answer by SamAgain Thursday, August 19, 2010 9:13 AM
    Wednesday, August 11, 2010 5:24 AM
  • Hi,

    Duplicated thread: http://social.msdn.microsoft.com/Forums/en-US/clr/thread/9ecd1ea4-a5d4-4f4f-9169-d19abb7bc86f


    Please mark the right answer at right time.
    Thanks,
    Sam
    Wednesday, August 11, 2010 8:38 AM
  • We temporarily mark a reply, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by Rudra2010 Friday, August 20, 2010 7:21 AM
    • Unmarked as answer by Rudra2010 Friday, August 20, 2010 7:21 AM
    Thursday, August 19, 2010 9:14 AM