Re: Can I create my own User events?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hello. I know how to setup events for actions based on html elements,
but is it possible to create my own?  For instance, if I execute a
function, then I can write a line of code to execute my own
specialized event.  I would also like to pass it parameters.

Unfortunately, on both JScript and JScript.NET it's not possible to define own events. however, you may override an event derived from a base class.


However, if you do not need to interop with other languages, you may simply emulate the events infrastructure. Events are no more than a syntax sugar, you know.

For example, you want to define an event called "Happens". Add those things:

• Some storage for the event handlers. These would be delegates, either of ..NET or ActiveX style, depending on the situation. This could be a list, an array, or a hash map.
• A function for subscribing to event, the standard name would be add_Happens(handler). Add the handler to the list.
• A function for unsubscribing from the event (optional), the standard name would be remove_Happens(handler). Remove the handler from the list. Note that .NET compares the handles semantically, not by instances.
• A function to fire the event. For example, OnHappens.


How when someone wants to subscribe for your events, he should call the add_Happens(MyHandler), where MyHandler is a memeber function with the arguments appropriate.

When the event source wants to fire the event, it calls the OnHappens with some arguments to be casted along with the event. The OnHappens function enumerates the handlers list and invokes each one with the appropriate set of arguments.

L> An "event" is something that happens, like a mouse click.
L> It's not something that you can execute.

It's a wrong understanding of the events nature. One who handles the mouse click executes the event (fires) so that the event handlers could sink it. You may as well call onclick() on the HTML element (or click() ? don't remember). Also, the event may fire as a result of an async operation completion, and so on. It's just an abstract conscept, like function, property, member variable, public/protected members, and so on, with its own use cases.

(H) Serge



.



Relevant Pages

  • Re: Method call order
    ... two possible handlers for it. ... The signal detector should, ... first from it and execute it. ... With proper care you can even change the sequence ...
    (borland.public.delphi.language.objectpascal)
  • Re: Controling Event Sequencing...
    ... have it execute AFTER the OnClick in completely done ... ... control exposes this event to the programmer to allow him / her to capture ... immidiately fire after the programmer's - ItemChanged event ... ... I can use my custom event ueMyEventChanged to access the ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Controling Event Sequencing...
    ... need the OnPostClick event fire after the OnClick event is completely ... event queue and have it execute AFTER the OnClick in completely done ... ... control exposes this event to the programmer to allow him / her to ... I inherit from this and create another button... ...
    (microsoft.public.dotnet.languages.vb)
  • PowerShell Eventing Library 0.5 Beta Released
    ... Events in PowerShell - but how? ... us wire up scriptblocks to .NET events as handlers quite easily, ... to execute our script. ...
    (microsoft.public.windows.server.scripting)
  • Signal handling in objects. Good idea? Best practices?
    ... Is it "right" to try to define signal handlers in a class, ... this be handled elsewhere such as the implementation code? ... I am trying to write a class where instances will execute a "cleanup" ...
    (comp.lang.perl.misc)