Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sharing events with multiple programs

Sharing events with multiple programs

Scheduled Pinned Locked Moved C#
questioncsharphelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aalex675
    wrote on last edited by
    #1

    I am trying to write a DLL that has an event and am subscribing to the event from 2 different programs, but each of my programs is creating a new instance of the DLL. The class is a singleton class and looks like this:

    public sealed class clsEvents
    {
    // Create a new Singleton Instance of the class
    public static readonly clsEvents Instance = new clsEvents();

    private string _Value = "";
    public event EventHandler ValueChanged;

    public void OnValueChanged(EventArgs e)
    {
    EventHandler handler = ValueChanged;
    if (null != handler)
    {
    handler(this, e);
    }
    }

    public string Value
    {
    get { return _Value; }
    set
    {
    _Value = value;
    OnValueChanged(EventArgs.Empty);
    }
    }
    }

    The singleton class only does half the work so that the same instance of the class is shared within each program but I need to use the same instance in both programs. I have set both programs to compile into the same directory so I know they are loading the same DLL but they are each creating their own instance of the class. Finally, my question: In VB6 it is possible to set the Instancing of a DLL to Multiuse and any programs that use that DLL would share the same global variables from the DLL. Is it possible to do the same thing in C# so that each of my programs uses the same instance of the singleton class? Forgive me if I am going about this all wrong but hopefully someone can help. Please tell me if there is a better way to share events across multiple programs.

    R A 2 Replies Last reply
    0
    • A aalex675

      I am trying to write a DLL that has an event and am subscribing to the event from 2 different programs, but each of my programs is creating a new instance of the DLL. The class is a singleton class and looks like this:

      public sealed class clsEvents
      {
      // Create a new Singleton Instance of the class
      public static readonly clsEvents Instance = new clsEvents();

      private string _Value = "";
      public event EventHandler ValueChanged;

      public void OnValueChanged(EventArgs e)
      {
      EventHandler handler = ValueChanged;
      if (null != handler)
      {
      handler(this, e);
      }
      }

      public string Value
      {
      get { return _Value; }
      set
      {
      _Value = value;
      OnValueChanged(EventArgs.Empty);
      }
      }
      }

      The singleton class only does half the work so that the same instance of the class is shared within each program but I need to use the same instance in both programs. I have set both programs to compile into the same directory so I know they are loading the same DLL but they are each creating their own instance of the class. Finally, my question: In VB6 it is possible to set the Instancing of a DLL to Multiuse and any programs that use that DLL would share the same global variables from the DLL. Is it possible to do the same thing in C# so that each of my programs uses the same instance of the singleton class? Forgive me if I am going about this all wrong but hopefully someone can help. Please tell me if there is a better way to share events across multiple programs.

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      I'm afraid I think you're out of luck with this. Each of your programs runs in its own process and the operating system ensures that processes are independant of one another. Your dll will be loaded twice - once into each process, and even though you have implemented a singleton you ultimately have two of them on your computer. You need to look at inter-process communication, something like named pipes perhaps or you might be able to get away with Mutexs. Hopefully someone here might have done this before.

      Regards, Rob Philpott.

      1 Reply Last reply
      0
      • A aalex675

        I am trying to write a DLL that has an event and am subscribing to the event from 2 different programs, but each of my programs is creating a new instance of the DLL. The class is a singleton class and looks like this:

        public sealed class clsEvents
        {
        // Create a new Singleton Instance of the class
        public static readonly clsEvents Instance = new clsEvents();

        private string _Value = "";
        public event EventHandler ValueChanged;

        public void OnValueChanged(EventArgs e)
        {
        EventHandler handler = ValueChanged;
        if (null != handler)
        {
        handler(this, e);
        }
        }

        public string Value
        {
        get { return _Value; }
        set
        {
        _Value = value;
        OnValueChanged(EventArgs.Empty);
        }
        }
        }

        The singleton class only does half the work so that the same instance of the class is shared within each program but I need to use the same instance in both programs. I have set both programs to compile into the same directory so I know they are loading the same DLL but they are each creating their own instance of the class. Finally, my question: In VB6 it is possible to set the Instancing of a DLL to Multiuse and any programs that use that DLL would share the same global variables from the DLL. Is it possible to do the same thing in C# so that each of my programs uses the same instance of the singleton class? Forgive me if I am going about this all wrong but hopefully someone can help. Please tell me if there is a better way to share events across multiple programs.

        A Offline
        A Offline
        Alaric_
        wrote on last edited by
        #3

        I'm not certain of my solution...I'm just trying to throw ideas your way. What about exposing this as a web service and having it influence a state machine on your server? Then, you could just proxy into the service in both apps and whenever you would trigger the event from inside each of the programs, it would actually fire the event in a single location. But again, I might be oversimplifying your specific problem.

        A 1 Reply Last reply
        0
        • A Alaric_

          I'm not certain of my solution...I'm just trying to throw ideas your way. What about exposing this as a web service and having it influence a state machine on your server? Then, you could just proxy into the service in both apps and whenever you would trigger the event from inside each of the programs, it would actually fire the event in a single location. But again, I might be oversimplifying your specific problem.

          A Offline
          A Offline
          Alaric_
          wrote on last edited by
          #4

          ...a web service is my preferred choice for allowing concurrent execution of a shared process. They're fairly straight forward and since replies from the server are sent as SOAP, they can be consumed by pretty much any language you want to work in.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups