none
Problems with invoking an event

    Dotaz

  • Hello,
    we are trying to use .NET C# dlls in Matlab using new Matlab .NET Interface. It's is working and we can use .NET objects in Matlab. Now we try to catch invoked  .NET event in Matlab. It works for events from .NET Framework, but it doesn't work for our objects.
    I define and invoke event like this:

    namespace TestEvent

    {

    public class Event

    {

    public event EventHandler eventInvoked;

    public bool InvokeEvent()

    {

    if (eventInvoked == null)

    return false;

    foreach (Delegate de in eventInvoked.GetInvocationList())

    {

    MessageBox.Show(de.Method.Name);

    MessageBox.Show("Target: " + de.Target.ToString());

    }

    eventInvoked(null, EventArgs.Empty);

    return true;

    }

    }

    }

    Using the MessageBoxes I can see, that I have added the listener from Matlab properly, but the matlab doesn't not catch any event.

    Bui if I add listener in Matlab to some of .NET Framework's class (e.g. System.IO.FileSystemWatcher.Changed) it is working and I can catch the event in Matlab.

    So I would like to know how native .NET classes invoke events, how should I invoke the event, or if you don't have any other idea what can be the problem.

    Thanks for any help. Eva

     

    24. října 2009 15:46

Odpovědi

  • Hello Eva,

    I dont know, if I really know, where is the problem and what you want to do, but generally to make your clas working, change row

    eventInvoked(null, EventArgs.Empty);

    to: eventInvoked.Invoke(this, EventArgs.Empty);

    Then instantiate your object

    EventClass eClass = new EventClass();
    eClass.eventInvoked += new EventHandler(eClass_eventInvoked);
    eClass.InvokeEvent();


    public void eClass_eventInvoked(object source, EventArgs e)
    {
        ---->    here you wil catch your invocation
    }



    namespace TestEvent

    {

    public class EventClass

    {

    public event EventHandler eventInvoked;

    public bool InvokeEvent()

    {

    if (eventInvoked == null)

    return false;

    foreach (Delegate de in eventInvoked.GetInvocationList())

    {

    MessageBox.Show(de.Method.Name);

    MessageBox.Show("Target: " + de.Target.ToString());

    }


    eventInvoked.Invoke(
    this, EventArgs.Empty);

    return true;

    }

    }

    }


    Best regards
    ME


    22. prosince 2009 13:24