Tag Info

Hot answers tagged

6

At this point, it is mostly a (pretty strong) convention. That is, it will be weird if you write a library that does not follow that convention. The Event Design Guidelines say: DO use object as the type of the first parameter of the event handler, and call it sender. However, you may note that current guidance says that you should not define your own ...


5

Promises were made to solve problems like this; they work really well in functional languages (I've personally used them extensively in Javascript, where curiously jQuery actually has the worst implementation of them - see this comparison). The weird thing about using promises is accepting that things are easier when you make everything use them. I will ...


4

There's a third option: mock the object with the event and use behavior testing to verify the mock's subscriptions at given points. Several mocking frameworks allow for this. public interface INeedToBeMocked { public event EventHandler EventRaised; } NSubstitute var mockedItem = Substitute.For<INeedToBeMocked>(); ...


3

Of the now partially outdated but still useful article from MSDN: "When to Use Delegates Instead of Interfaces (C# Programming Guide)", a few of the rules-of-thumb stand out: Use a delegate in the following circumstances: (3) The caller has no need to access other properties, methods, or interfaces on the object implementing the method. My ...


3

Yes, I believe your assumption is correct. I also tried to write some (ugly) code to test this hypothesis: class MyClassWithEvent { public event Action Fired; void OnFired() { var d = Fired; if (d != null) d(); } } class MyClassSubscribing { void M() { } public void DoSubscribe(MyClassWithEvent mcwe) { mcwe.Fired += M; ...


2

It is operating system specific. On Linux, the X11 server manage a tree of X-windows (a tree hierarchy of nested rectangular areas). So the X server knows which X-window is concerned by a mouse click. If you have access to a Linux + Xorg system, try the xev command. X11 might be replaced by Wayland in the future. Underneath, the X11 server program itself ...


2

When sending messages/events across a system, you should use specialized data messages for the payload. If not, if for example your user class changes, everything that listens to messages that have a user as payload, could be affected by that change as well. As to your question about normalizing: it's not because the signals share the same payload now, ...


2

Is there a better way to solve this problem? Yes! C# allows you to override the += and -= syntax. I'm not sure how easy it is to supply in common mocking frameworks like Moq, but it should be trivial to build your own fake object that has hooks into the subscribe and unsubscribe methods of your interface: private Action foo = () => {}; ...


2

I read a blog post on this a while ago: http://www.patrick-wied.at/blog/image-protection-on-the-web His approach is to use interlacing images instead of single, still-standing images. You'd split up the image into several fractions, for example in the form of stripes, and display them alternating very fast. That way, if you try to cap the screen, you will ...


1

I'm not sure I understood your question, but with the introduction of generics you don't need anymore to write a custom eventhandler, but you can create a custom EventArgs... public event EventHandler<MyEventArgs> MyEvent; public class MyEventArgs : EventArgs { //your stuff here } if you really need a custom EventHandler you can write: public ...


1

What you are describing is a common issue in reactive programming. The idiom is for asynchronous code to produce a Future which acts as a rendezvous point for other code. The code that depends on the results uses call-backs (or call-forwards, depending on your point of view), which take the value as it will be produced in the future. With suitable language ...


1

Why shouldn't event listeners outlive the object that registered them? It seems like you are assuming event listeners should registered by methods of controls(if we take the GUI example) - or more precisely, methods by objects of classes that inherit the GUI toolkit's controls. That's not a necessity - you could, for example, use a specialized object for ...


1

There is a subtle, but important difference between commands and events. A command has a presumption of a response whereas an event does not presume a response, it's merely a statement. To be less abstract: ShipOrder is a command and the sender of ShipOrder will likely expect a response of some sort. OrderShipped is a declaration and the sender is ...



Only top voted, non community-wiki answers of a minimum length are eligible