c# 跨应用程序域通讯

c# 跨应用程序域通讯,第1张

概述public class MyTask { public class MyEventArgs : EventArgs { public object EventData { get; private set; } public MyEventArgs(object eventData)
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> 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# 跨应用程序通讯全部内容,希望文章能够帮你解决c# 跨应用程序域通讯所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1225144.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存