Prism CompositePresentationEvent fires twice - c#

In my infrastructure i publish a event
this.eventAggregator.GetEvent<ReportAddedEvent>().Publish(report);
the report is a object
In my controller i subscribe to this event
this.eventAggregator.GetEvent<ReportAddedEvent>().Subscribe(this.OnReportAdded);
My problem is that the event fires twice. There is no other place in the entire code where the event is published so im certain that the event is not fired somewhere else and i can see it only fires once.
Anyone have a suggestion or have a solution to problem or knows where the problem lies.

I think the problem is that the code
this.eventAggregator.GetEvent<ReportAddedEvent>().Subscribe(this.OnReportAdded);
to subscribe to the ReportAddedEvent is executed two times.
You should check (by using a debugger and a breakpoint on the line) if it executes more than once.

Related

Navigating to the method behind an event in Visual Studio

I am working with some legacy code that uses a lot of events. Basically, every method is called throught an event. All those events have only one subscriber.
When reading some code that raises an event, how can I quickly navigate to the code of this one subscriber?
e.g I am reading trough some code:
event xy DoStuff_Event;
void foo()
{
DoStuff_Event()
}
Now I want to know what code would be executed here.
So I click on DoStuff_Event()->"Find all references"
Then I search the references for the line DoStuff_Event += DoStuff_ForReal()
(There is always only one such line in this codebase)
Then I click on DoStuff_ForReal()->"Go to definition"
Now I finally can read the code that will be executed when DoStuff_Event() is called. Usually this code will consist of several further event calls, which in turn call even more events, so I repeat the whole process.
Is there a faster way to jump from the line DoStuff_Event() to the method definition of DoStuff_ForReal()?
For my case, when there is always only one subscriber for each event, it seems like this search could be automated.

Understanding Event Handlers in lync

im very new to C# as well as lync SDK, can sumone help me to understand the meaning of the below line.
what i understood was whenever lefthand side event occures it will call the rightthand side handler. am i correct?
lyncClient.ConversationManager.ConversationAdded += ConversationManager_ConversationAdded;
You add an event handler called ConversationManager_ConversationAdded to a structure associated with event ConversationAdded. When a ConversationAdded event occurs, all handlers added will be called with arguments defining context of this event occurence.

C#.NET mnemonics unexpected behaviour

I have few buttons in my windows application which has mnemonics.Now if i press multiple keys the events of the button clicks gets fired as i have used mnemonics.
But the behaviour is not as expected,because second button event handler is getting executed before the first button event handler has finished its execution,and also i have written my code in such a way that in the event handler of first button i am disabling my second button,still the second button event handler is getting executed.The problem here is due to mnemonics.PLease suggest me a better way to handle mnemonics as soon as possible.
Thanks in advance.
If your logic is tied to your buttons, change that. Let your buttons merely manipulations an object that implements your logic. You can then check in this object whether the requested action is allowed (i.e. whether the previous operation has finished.
Alternatively you can just disable buttons when running and re-enable then when.finished.

Events Firing Unexpectedly

Can you write code to tell an event not to fire? Sometimes in my applications events fire unexpectedly. I know this is a broad question, but it must have happened to others out there.
You could put an if-statement in your event handler around all the code or you could dynamically detach and reattach the event handlers from outside the event handler, if necessary.
But really, events don't just fire without reason. You probably have something else going on. Maybe you can explain more what the situation is?
There is no standard way to suppress an "unexpected" event. But you could remove all of the event handlers. This is rather drastic, but would be equivalent to suppressing the event.
Alternatively you could add code to the event handlers to do nothing when the event is fired unexpectedly. This assumes you can define "unexpected".
Rather than suppress an event, a better approach is to remove/suppress whatever is that's causing the event to fire.

How to break when an event occurs, or determine associated handlers?

I'm using a large open-source control and I can't seem to find the code that handles a double-click event. Could I perhaps have the debugger break when a double-click occurs or otherwise learn what code is associated with that event?
Well, I stumbled around until I found the code I was looking for.
BTW, before the DoubleClick event occurs, a MouseDown event occurs for the second click, with MouseEventArgs.Clicks == 2.
The event you're probably looking for is Control.MouseDoubleClick, which is raised whenever a control is double-clicked by the mouse. Or, if you're deriving from Control, you can override OnMouseDoubleClick.

Categories