When I was doing some UI testing, I found few common exceptions.
For example when calling handler (directsoundout.cs, line 451):
// Fire playback stopped event
if (PlaybackStopped != null)
{
if (this.syncContext == null)
{
PlaybackStopped(this, EventArgs.Empty);
}
else
{
syncContext.Post(state => PlaybackStopped(this, EventArgs.Empty), null);
}
}
it should be written like this, because syncContext.Post will sooner or later throw nullreferenceexcepion. Playbackstopped could be null at this time.
var handler = PlaybackStopped;
// Fire playback stopped event
if (handler != null)
{
if (this.syncContext == null)
{
handler(this, EventArgs.Empty);
}
else
{
syncContext.Post(state => handler(this, EventArgs.Empty), null);
}
}
I think all the code needs revision , which is not so big deal. I could help you to write some UI, with UIautomation testing, which I think is the best way to test stuff like this, especially when it comes to thread safety.
Comments: this can only fail if someone is unsubscribing to the event from another thread while it is being called on another, but point taken, it is safer to do it the way you suggest.
For example when calling handler (directsoundout.cs, line 451):
// Fire playback stopped event
if (PlaybackStopped != null)
{
if (this.syncContext == null)
{
PlaybackStopped(this, EventArgs.Empty);
}
else
{
syncContext.Post(state => PlaybackStopped(this, EventArgs.Empty), null);
}
}
it should be written like this, because syncContext.Post will sooner or later throw nullreferenceexcepion. Playbackstopped could be null at this time.
var handler = PlaybackStopped;
// Fire playback stopped event
if (handler != null)
{
if (this.syncContext == null)
{
handler(this, EventArgs.Empty);
}
else
{
syncContext.Post(state => handler(this, EventArgs.Empty), null);
}
}
I think all the code needs revision , which is not so big deal. I could help you to write some UI, with UIautomation testing, which I think is the best way to test stuff like this, especially when it comes to thread safety.
Comments: this can only fail if someone is unsubscribing to the event from another thread while it is being called on another, but point taken, it is safer to do it the way you suggest.