Skip navigation

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).

The Problem

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:

scripterror

What the fuck was this?!? I put in error trapping …. still happened. I put in try catches in the JavaScript in the hosting page …. still happened!

The Solution

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:

[DllImport("ole32.dll")]
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)
{
webBrowser1.Navigate(“about:blank”);
System.Windows.Forms.Application.DoEvents();
CoFreeUnusedLibraries();
}

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:

http://savij.com/2007/06/19/webbrowser-control-and-sharepoint-crash-at-close/

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: