Monday, August 17, 2009

How is this possible ?

OK, so I have a media player control in my WPF app. For reasons that I cannot fathom, WPF does not let me easily find out the native size of a video, so I have added a map of video paths to sizes. I have built this map, and when I load a video, I grab a screenshot of the first frame. After that, you can play and pause the video, when you pause, it goes to the static screenshot, so that it will appear in screen grabs, which we print and email.

if (MediaAccessManager.Instance.VideoSizeMap.ContainsKey(srcPath))

This is the line of code I use to check if I have a size for this video in my map ( users can import their own videos and then I need to just use DirectX via a third party dll to find the size. I don't do this for my own videos, because sometimes the size reported, is incorrect. Here is the issue. The first time this code runs, I get an exception, stating that the key in question already exists in the collection. Believe me, I have read this code over and over, there is NO code here to put anything into the collection, nor does this crash after the first time. The only possible reason I can think of, is that perhaps the collection does not blow up when I put in a duplicate key, it blows up the first time I try to access it after the fact. However, I call ContainsKey before adding each item, and I have written some code to further confirm that I have no duplicate keys. I also wrote code to deliberately add the same key twice, and it threw an exception at the point of adding the item, as you'd expect.

I really don't have time to debug this, so I've added the block of code above in it's own try/catch. The first time this runs, it throws an exception, but then it does it again right away, and the second time it works fine, causing my code to work perfectly. It's just not at all pretty.

No comments:

Post a Comment