We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,818 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Events trigger order?

I have a custom control that derives directly from CheckedListBox. If the CheckedListBox subscribes to its own event (ItemCheck for example) and the user of the control also subscribes to it, does the control's event trigger first before the user's?

I read on MSDN that if multiple subscribers subscribe to an event, they are triggered synchronously, but I wasn't sure if that applied in this situation or not. I understand if there are multiple handlers for one event, but not sure about this one, if the event handlers occured synchronously on the control first and THEN the user's events are triggered synchronously.

Any ideas? I've tested it (kinda) with messageboxes and they always seem to trigger in the same order (but that doesn't mean it always will) with the control's event triggering first, and the form's event handlers triggering afterwards.

4
Contributors
7
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
8
Views
Question
Answered
zachattack05
Posting Pro
575 posts since Dec 2009
Reputation Points: 66
Solved Threads: 16
Skill Endorsements: 0

Not sure, but can you test it by temporarily putting a MessageBox in each handler to see which one fires first?

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

I tried that, and it always triggers in the same order, but I'm worried that it could just be a coincidence since they are supposed to execute synchrously.

zachattack05
Posting Pro
575 posts since Dec 2009
Reputation Points: 66
Solved Threads: 16
Skill Endorsements: 0

Could try a message box displaying a datetime down to milliseconds to see which executes first? Should be a slightly noticeable difference in the millisecond portion of the time?

MikeyIsMe
Master Poster
798 posts since Nov 2010
Reputation Points: 88
Solved Threads: 69
Skill Endorsements: 10

You could also handle both events and make each one call the same (separate ) method to do what you want.
Does this have to do with your new thread where setting the checkedListBox1.CheckOnClick = true;
could solve it?

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

thines01,

No that's a different issue, this one involves if an item is checked, a stack object must be updated with some data, and I would like to make sure that the stack is updated BEFORE any user code is executed.

Order example:

  1. User checks item
  2. ItemCheck event is triggered
    A. Since both the control itself and the user using the control subscribe to the ItemCheck event, I would like to make sure the order is as follows:
  3. Stack is updated with data
  4. User event code is executed

If 3 and 4 are reversed, and the user code needs to access the information in that stack, they could be provided with incorrect data.

Also, when I say "user" I mean the programmer using the control on a form, not a end-user of the software product.

zachattack05
Posting Pro
575 posts since Dec 2009
Reputation Points: 66
Solved Threads: 16
Skill Endorsements: 0

I think internally the delegates that subscribe to events are held in a list. Therefore they are fired in the same order as they are added, from start to finish (or maybe finish to start but almost certainly 1 of the 2). If you are really worried, write a program that simulates hundreds of event subscribers and save in a list the order they are fired. Then run an analysis on this data to see if is consistent.

skatamatic
Posting Shark
986 posts since Nov 2007
Reputation Points: 403
Solved Threads: 132
Skill Endorsements: 1

Hey that's a good idea!

I think I'll do that!

zachattack05
Posting Pro
575 posts since Dec 2009
Reputation Points: 66
Solved Threads: 16
Skill Endorsements: 0
Question Answered as of 1 Year Ago by thines01, MikeyIsMe and skatamatic

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0835 seconds using 2.65MB