Reputation: 43
Say I have an event with 2 subscribers (everything occurs in the same thread) - one subscriber writes to a log file, the other shows a MessageBox.
If the MessageBox is the first on the subscription list, then the log entry is not written until the after the user closes the message box. So the time in the log entry will really be the time the message box was closed, not the time the event occurred.
It seems the best solution is to have the log writer subscribe to the event before the code that displays the message box. However, in a similiar question here: Are event subscribers called in order of subscription?
the best answer was to never rely on the order of the subscribers. So how do I prevent the conflict without worrying about their order?
Upvotes: 3
Views: 540
Reputation: 13048
Add the timestamp as a property of your EventArgs
class. Write that timestamp to the log file, forget about the clock time at the moment the log happens to be written. That way it truly represents the moment the event was raised, not the arbitrary time at which the log file was written.
Upvotes: 0
Reputation: 1503489
EDITED:
Are you in control of the event code? If so, you can make sure it's never implemented in a pathologically weird way which reorders things. You can even document that as part of the event itself: "Handlers to this event are always called in subscription order, synchronously."
To be honest, I'd really expect any event which didn't go along with that to explicitly document it.
Upvotes: 0
Reputation: 4375
According to the documentation on events in the MSDN C# programming guide, events have the following properties (key point is bold):
Looks like the best bet is to use BeginInvoke on the events.
Upvotes: 0
Reputation: 41388
All of the individual event subscribers need to play well with others. The proper thing is for the event that shows the MessageBox to launch a background thread and show the MessageBox from there.
Upvotes: 1