Results 1 to 10 of 10

Thread: [2.0] plugin events

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    [2.0] plugin events

    I know how to (almost) create a plugin

    you need to create an interface which a plugin implements.
    You also need to create an interface which allows the "host" to be known to the plugin (your application)

    thats all cool.


    But now, how do I register events from the plugin to the main application (host) so that if I raise an event in the host, the plugins who have these events will also be notified/alerted/executed of the event?

    I have a delegate which has a string signature:

    public delegate void DoSomething(string text);

    event:

    public event DoSomething OnEventDoSomething;


    the event is in the plugin, and an eventhandler has been created.

    but now after loading the assembly/instantiating the plugin from the host, how can I register the event and execute it?

    Thanks

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: [2.0] plugin events

    =) I was working on this exactly but had to place it on hold. It appears that you can simply declare the event handler in the interface like usual, override it and then call it from the master just as you normally would, from what I've seen.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [2.0] plugin events

    yeh thats right, which is what ive discovered but now im trying to figure out how to raise the event from the app domain (host/main app) so the plugin's can act upon th event in their way

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: [2.0] plugin events

    The "gotcha" here is that an event must be raised by the class in which it is declared.

    So if you have a "Host" class with the event "OnSomething", you cannot raise this event from your "MyProgram" class.

    If you're having problems syntactically raising events, the syntax is like so:


    Code:
    // the signature for the handler.
    public delegate void SomethingEventDelegate(int arg1, string arg2);
    public SomethingEventDelegate SomeEvent;
    
    // ...
    // if there are handlers "connected" to this event
    if (SomeEvent != null)
         SomeEvent(42, "hello");
    
    
    // ...
    // some class subscribes to the event.
    otherClass.SomeEvent += new SomethingEventDelegate(myHandler);
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [2.0] plugin events

    huh? lol

    so if you have an event in your application domain (main program) and you raise it, and this event is also declared in an interface which a plugin has included, is there no way to raise this event from the main app so that the plugin can listen to it and act upon it?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: [2.0] plugin events

    Well, you could chain the events --

    the plugin can subscribe to a proxy event in the host object.
    the host object can subscribe to an event which will be raised by the main application. When this event is fired, the handler in the host object will simply raise it's proxy event to pass the event on to the plugin.

    Another option is to have your host have a function like "GetMainClass()" which returns an object of your main class. Then your plugin can manipulate this object directly. The issue here is that the plugin has to "know" about the main class - so you need to have it in an assembly that the plugin can reference.

    For a project of mine, a chat client, the I had a class layout like this:

    MainAssembly (dll) - contains the ChatClient class.
    MainProgram (exe) - simply instantiates a ChatClient.
    PluginInterface (dll) - contains the IPlugin interface, and the host object.
    Plugins (dll) - reference MainAssembly and PluginInterface in order to interact with the main object.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [2.0] plugin events

    complicated im never gonna get this

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [2.0] plugin events

    after having some food.... lets see how it goes.

    This is the code I have

    //Interface (IPlugin)
    Code:
    public delegate void DoSomething(string text);
    ..
    
    string TheAuthor { get; }
    string TheDescription { get; }
    IPluginHost TheHost { get; set; }
    
    event DoSomething OnEventDoSomething;
    
    void DoInitialize();
    void DoRegister(IPlugin thePlugin);
    void DoShutdown();
    //Plugin

    Code:
    public class ThePlugin : IPlugin
    ..
    //properties here
    ..
    
    public event DoSomething OnEventDoSomething;
    
    void DoInitialize();
    {
       this.OnEventDoSomething += new DoSomething(ThePlugin_OnEventDoSomething);
    }
    
    
    void ThePlugin_OnEventDoSomething(string text)
    {
       Console.Write("Event raised in the plugin, msg: " + text);
    }

    //Main Application (Host)
    Code:
    ....
    //instantiated Plugin
    
    IPlugin thePlugin = (IPlugin)Activator.CreateInstance(theType);
    this.DoRegister(thePlugin);
    thePlugin.DoInitialize();
    this.theCollectionOfPlugins.Add(thePlugin);
    
    //here for example, how do I now raise an event of this plugin so that the plugin(s) can catch it and act on it?


    I hope that helps

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  9. #9
    Member
    Join Date
    Sep 2005
    Posts
    44

    Re: [2.0] plugin events

    Quote Originally Posted by sunburnt
    the plugin can subscribe to a proxy event in the host object.
    the host object can subscribe to an event which will be raised by the main application. When this event is fired, the handler in the host object will simply raise it's proxy event to pass the event on to the plugin.
    Any idea of how to implement that? Or somewhere explains that?

    Thanks

  10. #10
    Member
    Join Date
    Sep 2005
    Posts
    44

    Re: [2.0] plugin events

    noone?

    i understand the idea, but don't know how to do this on c#. please, any suggestion?

    Thanks,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width