Event Handling

Oct 23, 2013 at 12:49 PM
I want to know whether I can subscribe and catch event events generated by custom object. How can I do it.
Coordinator
Oct 23, 2013 at 1:49 PM
Edited Oct 23, 2013 at 2:00 PM
Hello rohittaralekar!

Suppose you have a custom object with an event:
public class Foo
{
    public class OnDoSomethingArgs : EventArgs
    {
        public string Value;
    }

    public event EventHandler<OnDoSomethingArgs> OnDoSomething;

    public void DoSomething(string value)
    {
        if (OnDoSomething != null)
        {
            var args = new OnDoSomethingArgs { Value = value };
            OnDoSomething(this, args);
        }
    }
}
Here's a sample that shows how to use the event from script code:
engine.AddHostType("Foo", typeof(Foo));
engine.AddHostType("Console", typeof(Console));
engine.Execute(@"
    var foo = new Foo();
    var connection = foo.OnDoSomething.connect(function (s, e) {
        Console.WriteLine(e.Value);
    });
    foo.DoSomething('Hello, world!');
    connection.disconnect();
");
Cheers!
Oct 24, 2013 at 6:22 AM
Thank for your beautiful guidance. Still I am not clearly understood concept. I will tell you my scenario I have following classes:

Event :
public delegate void DivideByTwoEventHandler(object obj, DivideByTwoEventArgs e);
public class DivideByTwoEventArgs
{
  public int number;

  public DivideByTwoEventArgs(int num)
  {
     number = num;
  }
}

And class which contains this event is:
public class NumberOperation
{
  public event DivideByTwoEventHandler EventTwo;

  public void OnDivisibleByTwo(DivideByTwoEventArgs e)
  {
     if (EventTwo != null)
     {
        EventTwo(new object(), e);
     }
  }
  public void GetNumbers()
  {
     for (int i = 0; i < 99; i++)
     {
        if (i % 2 == 0)
        {
           DivideByTwoEventArgs e1 = new DivideByTwoEventArgs(i);
           OnDivisibleByTwo(e1);
        }
     }
  }
}

in above code how can call event of NumberOperation Class through javascript
Oct 24, 2013 at 6:23 AM
Edited Oct 24, 2013 at 6:36 AM
Please guide me.

NumberOperation numOperation = new NumberOperation();
     numOperation.GetNumbers(); //here my event will be raised..

How can i subscribe it through javascript
Coordinator
Oct 24, 2013 at 3:54 PM
Here's an example using the definitions you've provided:
// create the object
NumberOperation numOperation = new NumberOperation();

// expose the object to script code
engine.Script.numOperation = numOperation;

// subscribe; the handler writes the number to the console
engine.AddHostType("Console", typeof(Console));
engine.Execute(@"
    connection = numOperation.EventTwo.connect(function (s, e) {
        Console.WriteLine(e.number);
    });
");

// invoke the operation that raises events
numOperation.GetNumbers();

// unsubscribe
engine.Execute(@"connection.disconnect()");
Good luck!
Oct 25, 2013 at 6:49 AM
Edited Oct 25, 2013 at 7:07 AM
It's nice... Thank you very very much. This article helped me lot. Thanks for doing such beautiful work !!!! :-)