<commit_before>using System;
using System.IO;
using System.Runtime.InteropServices;

namespace ExcelDna.IntelliSense
{
    static class CrossAppDomainSingleton
    {
        private const string AppDomainName = "ExcelDNASingletonAppDomain";

        private static IntelliSenseDisplay _intelliSenseDisplay;
      
        private static AppDomain GetAppDomain(string friendlyName)
        {
            IntPtr enumHandle = IntPtr.Zero;
            mscoree.CorRuntimeHost host = new mscoree.CorRuntimeHost();

            try
            {
                host.EnumDomains(out enumHandle);

                while (true)
                {
                    object domain;
                    host.NextDomain(enumHandle, out domain);

                    if (domain == null)
                    {
                        break;
                    }

                    AppDomain appDomain = (AppDomain) domain;
                    if (appDomain.FriendlyName.Equals(friendlyName))
                    {
                        return appDomain;
                    }
                }
            }
            finally
            {
                host.CloseEnum(enumHandle);
                Marshal.ReleaseComObject(host);
            }

            return null;
        }

        public static IntelliSenseDisplay GetOrCreate()
        {
            Type type = typeof(IntelliSenseDisplay);

            AppDomain appDomain = GetAppDomain(AppDomainName);

            var xllName = Win32Helper.GetXllName();

            if (appDomain == null)
            {
                AppDomainSetup domaininfo = new AppDomainSetup();
                domaininfo.ApplicationBase = Path.GetDirectoryName(xllName);
                appDomain = AppDomain.CreateDomain(AppDomainName, AppDomain.CurrentDomain.Evidence, domaininfo);
            }

            _intelliSenseDisplay = (IntelliSenseDisplay)appDomain.GetData(type.FullName);

            if (_intelliSenseDisplay == null)
            {
                _intelliSenseDisplay = (IntelliSenseDisplay)appDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
                _intelliSenseDisplay.SetXllOwner(Win32Helper.GetXllName());
            }

            _intelliSenseDisplay.AddReference(xllName);

            return _intelliSenseDisplay;
        }

        public static void RemoveReference()
        {
            _intelliSenseDisplay.RemoveReference(Win32Helper.GetXllName());

            if (!_intelliSenseDisplay.IsUsed())
            {
                IntelliSenseDisplay.Shutdown();
                _intelliSenseDisplay = null;<commit_msg>Update CrossAppDomainSingleton.cs<commit_after>using System;
using System.IO;
using System.Runtime.InteropServices;

namespace ExcelDna.IntelliSense
{
    static class CrossAppDomainSingleton
    {
        private const string SingletonName = "ExcelDNASingleton";
        private const string AppDomainName = "ExcelDNASingletonAppDomain";

        private static IntelliSenseDisplay _intelliSenseDisplay;
      
        private static AppDomain GetAppDomain(string friendlyName)
        {
            IntPtr enumHandle = IntPtr.Zero;
            mscoree.CorRuntimeHost host = new mscoree.CorRuntimeHost();

            try
            {
                host.EnumDomains(out enumHandle);

                while (true)
                {
                    object domain;
                    host.NextDomain(enumHandle, out domain);

                    if (domain == null)
                    {
                        break;
                    }

                    AppDomain appDomain = (AppDomain) domain;
                    if (appDomain.FriendlyName.Equals(friendlyName))
                    {
                        return appDomain;
                    }
                }
            }
            finally
            {
                host.CloseEnum(enumHandle);
                Marshal.ReleaseComObject(host);
            }

            return null;
        }

        public static IntelliSenseDisplay GetOrCreate()
        {
            Type type = typeof(IntelliSenseDisplay);

            AppDomain appDomain = GetAppDomain(AppDomainName);

            var xllName = Win32Helper.GetXllName();

            if (appDomain == null)
            {
                AppDomainSetup domaininfo = new AppDomainSetup();
                domaininfo.ApplicationBase = Path.GetDirectoryName(xllName);
                appDomain = AppDomain.CreateDomain(AppDomainName, AppDomain.CurrentDomain.Evidence, domaininfo);
            }

            _intelliSenseDisplay = (IntelliSenseDisplay)appDomain.GetData(SingletonName);

            if (_intelliSenseDisplay == null)
            {
                _intelliSenseDisplay = (IntelliSenseDisplay)appDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
                _intelliSenseDisplay.SetXllOwner(Win32Helper.GetXllName());
                
                appDomain.SetData(SingletonName, _intelliSenseDisplay);
            }

            _intelliSenseDisplay.AddReference(xllName);

            return _intelliSenseDisplay;
        }

        public static void RemoveReference()
        {
            _intelliSenseDisplay.RemoveReference(Win32Helper.GetXllName());

            if (!_intelliSenseDisplay.IsUsed())
            {
                IntelliSenseDisplay.Shutdown();
                _intelliSenseDisplay = null;
{'fraction_non_alphanumeric': 0.06787497688181986, 'fraction_numerical': 0.002219345293138524, 'mean_word_length': 1.605009633911368, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1749678', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1283, 'n_words': 221}