Quantcast
Channel: naudio Work Item Rss Feed
Viewing all articles
Browse latest Browse all 738

Commented Issue: Thread safety [16330]

$
0
0
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.

Viewing all articles
Browse latest Browse all 738

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>