Click here to Skip to main content
15,888,984 members
Home / Discussions / C#
   

C#

 
QuestionApplication Domain Pin
dataminers14-Jan-10 23:24
dataminers14-Jan-10 23:24 
AnswerRe: Application Domain Pin
EliottA15-Jan-10 2:44
EliottA15-Jan-10 2:44 
AnswerRe: Application Domain Pin
Eddy Vluggen15-Jan-10 3:11
professionalEddy Vluggen15-Jan-10 3:11 
GeneralRe: Application Domain Pin
dataminers15-Jan-10 9:50
dataminers15-Jan-10 9:50 
GeneralRe: Application Domain Pin
Eddy Vluggen15-Jan-10 11:17
professionalEddy Vluggen15-Jan-10 11:17 
GeneralRe: Application Domain Pin
dataminers16-Jan-10 2:23
dataminers16-Jan-10 2:23 
GeneralRe: Application Domain Pin
Eddy Vluggen16-Jan-10 8:50
professionalEddy Vluggen16-Jan-10 8:50 
GeneralRe: Application Domain Pin
dataminers15-Jan-10 10:31
dataminers15-Jan-10 10:31 
Following two class in the ClassLibrary

FIRST CLASS

using System;
using System.Runtime.InteropServices;

namespace MyLibrary
{
    public class CrossAppDomainSingleton<T> : MarshalByRefObject where T : new() 
    { 
        private static readonly string AppDomainName = "Singleton AppDomain"; 
        private static T _instance;

        private static AppDomain GetAppDomain(string friendlyName) 
        { 
            IntPtr enumHandle = IntPtr.Zero; 
            mscoree.CorRuntimeHostClass host = new mscoree.CorRuntimeHostClass(); 
            
            try 
            { 
                host.EnumDomains(out enumHandle); 
                object domain = null; 
                
                while (true) 
                { 
                    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); 
                host = null; 
            }
            
            return null; 
        } 
        
        public static T Instance 
        { 
            get 
            { 
                if (null == _instance) 
                { 
                    AppDomain appDomain = GetAppDomain(AppDomainName); 
                    
                    if (null == appDomain) 
                    { 
                        appDomain = AppDomain.CreateDomain(AppDomainName); 
                    } 
                    
                    Type type = typeof(T); 
                    T instance = (T)appDomain.GetData(type.FullName); 
                    
                    if (null == instance) 
                    { 
                        instance = (T)appDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName); 
                        appDomain.SetData(type.FullName, instance); 
                    } 
                    
                    _instance = instance; 
                } 
                
                return _instance; 
            } 
        } 
    }
}


SECOND CLASS

using System.Collections.Generic;

namespace MyLibrary
{
    public class MySingleton : CrossAppDomainSingleton<MySingleton>
    {
        private List<string> m_List = new List<string>();

        public void Add(string pValue)
        {
            m_List.Add(pValue);
        }

        public List<string> Get()
        {
            return m_List;
        }

    }
}


I ADD TWO WindowsFormsApplication TO MY SOLUTION, AND I ADD REFERENCE WHICH IS MYLIBRARY

I WROTE FOLLOWING CODE TO FIRST WINFORM
MySingleton.Instance.Add("test");


I WROTE FOLLOWING CODE TO SECOND WINFORM
List<string> res = MySingleton.Instance.Get();


BUT I CAN GET ADDED ITEM FROM FIRST WINFORM IN SECOND WINFORM
QuestionCaching powers of 2 -- performance consideration Pin
Lutosław14-Jan-10 22:36
Lutosław14-Jan-10 22:36 
AnswerRe: BitVector performance question [modified] Pin
Covean14-Jan-10 23:00
Covean14-Jan-10 23:00 
GeneralRe: BitVector performance question Pin
Lutosław14-Jan-10 23:55
Lutosław14-Jan-10 23:55 
GeneralRe: BitVector performance question Pin
Covean15-Jan-10 0:09
Covean15-Jan-10 0:09 
GeneralRe: BitVector performance question Pin
Lutosław15-Jan-10 0:33
Lutosław15-Jan-10 0:33 
JokeRe: BitVector performance question Pin
Covean15-Jan-10 0:54
Covean15-Jan-10 0:54 
GeneralRe: BitVector performance question Pin
Lutosław15-Jan-10 0:04
Lutosław15-Jan-10 0:04 
GeneralRe: BitVector performance question Pin
Lutosław15-Jan-10 0:09
Lutosław15-Jan-10 0:09 
GeneralRe: BitVector performance question Pin
Luc Pattyn15-Jan-10 1:47
sitebuilderLuc Pattyn15-Jan-10 1:47 
AnswerRe: Caching powers of 2 -- performance consideration Pin
harold aptroot15-Jan-10 4:38
harold aptroot15-Jan-10 4:38 
GeneralRe: Caching powers of 2 -- performance consideration Pin
Lutosław15-Jan-10 11:24
Lutosław15-Jan-10 11:24 
GeneralRe: Caching powers of 2 -- performance consideration Pin
harold aptroot15-Jan-10 11:51
harold aptroot15-Jan-10 11:51 
QuestionGenerate Color Book File Format (.acb file) for Photoshop using .NET or Other Language Pin
shaktisinh14-Jan-10 19:13
shaktisinh14-Jan-10 19:13 
Questionhow to inflate GDI+ Regoins Pin
VCsamir14-Jan-10 19:02
VCsamir14-Jan-10 19:02 
QuestionAny one having code to compare two xml files....? Pin
koolprasad200314-Jan-10 18:48
professionalkoolprasad200314-Jan-10 18:48 
AnswerRe: Any one having code to compare two xml files....? Pin
VCsamir14-Jan-10 18:56
VCsamir14-Jan-10 18:56 
GeneralRe: Any one having code to compare two xml files....? Pin
koolprasad200314-Jan-10 20:39
professionalkoolprasad200314-Jan-10 20:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.