I know, it’s been a long time since I updated this blog. I try my best to only put things here that really stump people (including me).
Well, I was working on a WinForms project in c# which hosted a dialog with a .net Browser control in it. That control was pointed to an aspx page which hosted a Silverlight plug-in.
The Silverlight ran perfectly! If by perfectly I meant that I only used it once or twice. After a few pops of my dialog, I got the following error:
It turns out that I have seen something like this before, but where? Here is my thought process that triggered my memory. I noticed that the browser was not showing me my custom loading screen (or the default one once I took the custom one out). So, since the browser was not even downloading the xap file the problem was probably not mine. After all, it works the first few times but then wont even load the xap? Hmm, where have I seen this before?
So there is this awesome site that helped me with SharePoint crashing my browser control (oh wait that was this site <grin>). The problem was memory not getting released at the COM level in the browser control. It didn’t happen all the time and it didn’t happen on every win form, but it was consistent enough. The fact that the Silverlight would load and run just fine the first few times made me think that maybe the browser was getting tripped up on the Silverlight control and not releasing it’s hooks. Turns out that was the case!
Here is the fix:
Import the dll that holds CoFreeUnusedLibraries(), put this code at the top of your class just under the class declaration:
static extern void CoFreeUnusedLibraries();
Hook your FormClosing event (or some event you can call when closing your view) and do this:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
I added a call to Navigate so that the browser clears any current handles it might have and call DoEvents() just to give the browser a chance to clear out everything. You don’t see the browser change because it’s closing anyway.
That’s it! problem solved. Now wasn’t that worth the wait?
Here is a link to the previous post for more information if you like: