C# .NET AppDomains, How to check if Unloaded

This hack is a quick and easy way to determine if a specific domain is loaded or unloaded.

namespace AppDomains
{
    class Program
    {
        static void Main(string[] args)
        {
            string domainName = "MY DOMAIN NAME";

            MySerializedData serialData = new MySerializedData();

            //Create domain and assign a name.  The spawned domain can read it's own name.
            System.AppDomain domain = System.AppDomain.CreateDomain(domainName);

            //SetData applies a dictionary like Key, Value pairs to the domain.
            domain.SetData("MyDataObj", serialData);
            domain.SetData("stringData", "Hi there.");
            domain.SetData("intData", 999);


            //Processes which complete quickly and unload themselves, before DoCallBack completes, throw AppDomainUnloadedException 
            try
            {
                //Invoke, or start, the STATIC method via callback
                domain.DoCallBack(MyDomainCallback);
            }
            catch (System.AppDomainUnloadedException e)
            {
                System.Console.WriteLine("The appdomain " + domainName + " exited before DoCallBack completed.");
            }


            //HACK: To check if the appdomain is running.
            try
            {
                System.Console.WriteLine();
                // Note that the following statement creates an exception because the domain no longer exists.
                System.Console.WriteLine("Child domain: " + domain.FriendlyName);
            }
            catch (System.AppDomainUnloadedException e)
            {
                System.Console.WriteLine("The appdomain " + domainName + " does not exist.");
            }

            System.Console.ReadLine();
        }




        //Static Callback method for AppDomain.DoCallBack
        public static void MyDomainCallback()
        {
            //Get current domain
            System.AppDomain domain = System.AppDomain.CurrentDomain;

            string name = domain.FriendlyName;
            string threadID = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();

            //Get my data passed via key value pairs
            MySerializedData d = (MySerializedData)domain.GetData("MyDataObj");
            string stringData = (string)domain.GetData("stringData");
            int intData = (int)domain.GetData("intData");

            //Display in consoles
            System.Console.WriteLine("MyData={{{0},{1},'{2}'}}, stringData='{3}', and intData={4} were passed to {5}, on thread ID: {6}",
                d.a.ToString(),
                d.b.ToString(),
                d.d,
                stringData,
                intData.ToString(),
                name,
                threadID);


            //Self unloading
            System.AppDomain.Unload(domain);
        }
    }




    //Simple class used to pass data to the callback
    [System.Serializable]
    class MySerializedData
    {
        public int a = 3, b = 5;
        public string d = "xxx";
    }
}

David Gregory Medina

Post created by: David Gregory Medina

Tagged with: , , ,

Leave a Reply