代码之家  ›  专栏  ›  技术社区  ›  DaveG

如何在两个项目之间创建事件?

  •  0
  • DaveG  · 技术社区  · 7 年前

    我创建了两个项目( Proj_1 , Proj_2 ), 项目1 包含 Proj_1_Program.cs ProjectOneClass.cs , 项目2 包含 Proj_2_Program.cs ,我需要 OnInformed 触发两者 Informed1 Informed2 我就是这样走到现在的:

    //Proj_1_Program.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace CrossProjectEvent
    {
        class Proj_1_Program
        {
            static void Main(string[] args)
            {
                ProjectOneClass obj1 = new ProjectOneClass();
                obj1.Inform += new EventHandler(Informed1);
                obj1.InformNow();
                Console.ReadLine();
            }
    
            private static void Informed1(object sender, EventArgs e)
            {
                Console.WriteLine("Informed1");
            }
        }
    }
    
    //ProjectOneClass.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace CrossProjectEvent
    {
        public class ProjectOneClass
        {
            public event EventHandler Inform;
    
            public void InformNow()
            {
                OnInformed(new EventArgs());
            }
    
            private void OnInformed(EventArgs eventArgs) // I want this method both trigger Informed1 and Informed2
            {
                if (Inform != null)
                {
                    Inform(this, eventArgs); 
                }
            }
        }
    }
    
    
    
    //Proj_2_Program.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using CrossProjectEvent;
    
    namespace Proj_2
    {
        public class ProjectTwoClass
        {
            public ProjectOneClass obj_proj_1;
    
            public ProjectTwoClass()
            {
                obj_proj_1 = new ProjectOneClass();
                obj_proj_1.Inform += new EventHandler(Informed2);
            }
    
            private static void Informed2(object sender, EventArgs e)
            {
                Console.WriteLine("Informed2");
            }
        }
    
        class Project2
        {
            static void Main(string[] args)
            {
            }
        }
    
    }
    

    但似乎只有 信息1 被触发,如何修复?谢谢

    1 回复  |  直到 7 年前
        1
  •  0
  •   DeveloperExceptionError    7 年前

    这是一个需要进程间通信的典型问题。有十亿种不同的技术和方法可以实现这一点。

    一种解决方案是使用命名管道进行远程处理( Sample ),但也可以使用TCP和NetSockets。这可能是最简单的解决方案之一。

    如果您正在构建一个需要大量进程间通信的大型应用程序,那么参与者模型,尤其是 AKKA.NET 值得一提的是图书馆。

    但这些只是你的几个选择。