event-handling

C#: Thread-safe events

三世轮回 提交于 2019-11-29 07:40:46
Is the implementation below thread-safe? If not what am I missing? Should I have the volatile keywords somewhere? Or a lock somewhere in the OnProcessingCompleted method? If so, where? public abstract class ProcessBase : IProcess { private readonly object completedEventLock = new object(); private event EventHandler<ProcessCompletedEventArgs> ProcessCompleted; event EventHandler<ProcessCompletedEventArgs> IProcess.ProcessCompleted { add { lock (completedEventLock) ProcessCompleted += value; } remove { lock (completedEventLock) ProcessCompleted -= value; } } protected void OnProcessingCompleted

Events/Delegates In Java or C#

风流意气都作罢 提交于 2019-11-29 07:13:59
I've been trying to learn about events/delegates, but am confused about the relationship between the two. I know that delegates allow you to invoke different functions without needing to know what particular function is being invoked. (eg: a graphing function needs to accept inputs that are different functions to be graphed). But I don't see how delegates are used in Events. Can someone construct a simple example (in pseudocode or C# or Java) that illustrates the workings of Delegates as related to Events? Thanks! (This is all from a C# perspective.) I have an article about the differences

How to use delegates in correct way / Understanding delegates

痞子三分冷 提交于 2019-11-29 07:09:46
use - C# (.Net Framework 4.5, Visual Studio 2012) I try to understand such theme like Delegate, and currently I have few points, that must be clarified for me. I found a lot of different information in internet that describe how to use it, but it's a little bit complicated to understanding for me this theme. As I understand I must to do few thing for using delegate: Create some entity for work with it (that require creating some delegate) Declare a delegate type Create some method where I call to delegate In main class call delegate with required method that use entity (from first point) all

How to reference an event in C#

时光怂恿深爱的人放手 提交于 2019-11-29 07:09:20
I have the following class, which has one public event called LengthChanged : class Dimension { public int Length { get { return this.length; } set { if (this.length != value) { this.length = value; this.OnLengthChanged (); } } protected virtual void OnLengthChanged() { var handler = this.LengthChanged; if (handler != null) { handler (this, System.EventArgs.Empty); } } public event System.EventHandler LengthChanged; private int length; } I would like to be able to register/unregister handlers for this event in a method called Observer , which does not know anything about the Dimension class. I

Why does addEventListener fire before the event if at all? [duplicate]

烂漫一生 提交于 2019-11-29 06:57:05
This question already has an answer here: Javascript “addEventListener” Event Fires on Page Load [duplicate] 2 answers I was experimenting [in jsfiddle] w/a function created to append a newly created TextNode to the <p> in the HTML below: <button onclick="addTextNode('YES! ');">YES!</button> <button onclick="addTextNode('NO! ');">NO!</button> <button onclick="addTextNode('WE CAN! ');">WE CAN!</button> <hr /> <p id="p1">First line of paragraph.</p> Here is my javascript as well: function addTextNode(text) { var newtext = document.createTextNode(text), p1 = document.getElementById("p1"); p1

How to debug JavaScript / jQuery event bindings with Firebug or similar tools?

懵懂的女人 提交于 2019-11-29 06:31:44
I need to debug a web application that uses jQuery to do some fairly complex and messy DOM manipulation. At one point, some of the events that were bound to particular elements, are not fired and simply stop working. If I had a capability to edit the application source, I would drill down and add a bunch of Firebug console.log() statements and comment/uncomment pieces of code to try to pinpoint the problem. But let's assume I cannot edit the application code and need to work entirely in Firefox using Firebug or similar tools. Firebug is very good at letting me navigate and manipulate the DOM.

jQuery detect mousedown inside an element and then mouseup outside the element

别等时光非礼了梦想. 提交于 2019-11-29 06:18:29
问题 I have something similar to a drawing canvas, and I capture it's state on mouseup for undo purposes. The canvas isn't full screen, so you can draw with a brush and release outside the canvas. Something like this: $("#element").mousedown(function(){ $(document).mouseup(function(){ //do something }); }); But this doesn't work of course. A plain $(document).mouseup doesn't work either, because I have many other UI elements and it saves the state each time you click on a UI element. Any ideas?

event.keycode vs event.which

廉价感情. 提交于 2019-11-29 06:07:34
问题 I fell foul of a Firefox keydown behavior in that pressing the enter key (indeed any key) without having focus on a specific field will NOT trigger a keydown event it will only trigger a keypress event. This could be very confusing as the keydown and keyup event use JavaScript key codes whereas keypress uses ASCII codes. Fortunately 13 (enter/return) is common to both. Is there any known reason why FF using keypress in this circumstance? What is the benefit? Once this was established IE8

Creating Observable from normal Java events

偶尔善良 提交于 2019-11-29 05:44:42
问题 What is the best way to create an Rx-Java Observable from the classical Java event pattern? That is, given class FooEvent { ... } interface FooListener { void fooHappened(FooEvent arg); } class Bar { public void addFooListener(FooListener l); public void removeFooListener(FooListener l); } I want to implement Observable<FooEvent> fooEvents(Bar bar); The implementation I came up with is: Observable<FooEvent> fooEvents(Bar bar) { return Observable.create(new OnSubscribeFunc<FooEvent>() { public

C# dynamically add event handler

自作多情 提交于 2019-11-29 05:30:57
Hi i have a simple question. here is my code: XmlDocument xmlData = new XmlDocument(); xmlData.Load("xml.xml"); /* Load announcements first */ XmlNodeList announcements = xmlData.GetElementsByTagName("announcement"); for (int i = 0; i < announcements.Count; i++) { ToolStripMenuItem item = new ToolStripMenuItem(); item.Name = announcements[i].FirstChild.InnerText; item.Text = announcements[i].FirstChild.InnerText; /* HERE IS WERE I NEED HELP */ item.Click += new EventHandler(); this.freedomMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { item }); } The xml LastChild holds