c# 跨应用程序域通讯

public class MyTask
    {
        public class MyEventArgs : EventArgs
        {
            public object EventData { get; private set; }

            public MyEventArgs(object eventData)
            {
                this.EventData = eventData;
            }
        }

        public event EventHandler<MyEventArgs> MyEvent;

        public void DoWork()
        {
            Console.WriteLine("Start");
            var i = 0;
            while (i++ < 10)
            {
                Thread.Sleep(1000);
                var temp = this.MyEvent;
                if (temp != null)
                    temp(this, new MyEventArgs("MyEvent(" + i.ToString() + ")"));
            }
            Console.WriteLine("End");
        }
    }

    public static class NewAppDomain
    {
        /// <summary>
        /// 无参调用
        /// </summary>
        /// <param name="action"></param>
        public static void Execute(Action action)
        {
            AppDomain domain = null;

            try
            {
                domain = AppDomain.CreateDomain("New App Domain: " + Guid.NewGuid());

                var domainDelegate = (AppDomainDelegate)domain.CreateInstanceAndUnwrap(
                    typeof(AppDomainDelegate).Assembly.FullName,
                    typeof(AppDomainDelegate).FullName);

                domainDelegate.Execute(action);
            }
            finally
            {
                if (domain != null)
                    AppDomain.Unload(domain);
            }
        }

        /// <summary>
        /// 有参调用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameter"></param>
        /// <param name="action"></param>
        public static void Execute<T>(T parameter, Action<T> action)
        {
            AppDomain domain = null;

            try
            {
                domain = AppDomain.CreateDomain("New App Domain: " + Guid.NewGuid());

                var domainDelegate = (AppDomainDelegate)domain.CreateInstanceAndUnwrap(
                    typeof(AppDomainDelegate).Assembly.FullName,
                    typeof(AppDomainDelegate).FullName);

                domainDelegate.Execute(parameter, action);
            }
            finally
            {
                if (domain != null)
                    AppDomain.Unload(domain);
            }
        }

        /// <summary>
        /// 有返回值的无参调用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="action"></param>
        /// <returns></returns>
        public static T Execute<T>(Func<T> action)
        {
            AppDomain domain = null;

            try
            {
                domain = AppDomain.CreateDomain("New App Domain: " + Guid.NewGuid());

                var domainDelegate = (AppDomainDelegate)domain.CreateInstanceAndUnwrap(
                    typeof(AppDomainDelegate).Assembly.FullName,
                    typeof(AppDomainDelegate).FullName);

                return domainDelegate.Execute(action);
            }
            finally
            {
                if (domain != null)
                    AppDomain.Unload(domain);
            }
        }

        /// <summary>
        /// 有返回值的有参调用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="parameter"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static TResult Execute<T, TResult>(T parameter, Func<T, TResult> action)
        {
            AppDomain domain = null;

            try
            {
                domain = AppDomain.CreateDomain("New App Domain: " + Guid.NewGuid());

                var domainDelegate = (AppDomainDelegate)domain.CreateInstanceAndUnwrap(
                    typeof(AppDomainDelegate).Assembly.FullName,
                    typeof(AppDomainDelegate).FullName);

                return domainDelegate.Execute(parameter, action);
            }
            finally
            {
                if (domain != null)
                    AppDomain.Unload(domain);
            }
        }

        private class AppDomainDelegate : MarshalByRefObject
        {
            public void Execute(Action action)
            {
                action();
            }

            public void Execute<T>(T parameter, Action<T> action)
            {
                action(parameter);
            }

            public T Execute<T>(Func<T> action)
            {
                return action();
            }

            public TResult Execute<T, TResult>(T parameter, Func<T, TResult> action)
            {
                return action(parameter);
            }
        }
    }
private void Form1_Load(object sender, EventArgs e)
        {
            //1.无参无返回值调用,兼跨域事件调用
            NewAppDomain.Execute(() => 
            {
                var mytask = new MyTask();
                mytask.MyEvent += new EventHandler<MyTask.MyEventArgs>(mytask_IndexChanged);
                mytask.DoWork();
            });

            //2.有参无返回值调用
            var dt = DateTime.Now.ToString();
            NewAppDomain.Execute<string>(dt, args =>
            {
                Console.WriteLine(args);
            });

            //3.无参有返回值调用
            var result = NewAppDomain.Execute(() => { return "helloworld"; });

            //4.有参有返回值调用
            var parameter = new List<string> { "a", "b", "c" };
            var result2 = NewAppDomain.Execute(parameter, args => { return string.Join("|", args); });
        }

        void mytask_IndexChanged(object sender, MyTask.MyEventArgs e)
        {
            Debug.Print("Event:" + e.EventData.ToString());
        }

 

c# 跨应用程序域通讯

上一篇:win10 提示该文件没有与之关联的应用来执行该操作


下一篇:Genome Sequencing ofMuseumSpecimens Reveals Rapid Changes in the Genetic Composition of Honey Bees in California