开发者

Static reference to instance in IE Toolbar

I am having an interesting issue with a COM component written to function as a toolbar in IE. Basically if you open up several tabs at once in IE the individual instances of the COM objects get all twisted around. Bear with me here.

Say I open up five browser tabs all at once by right clicking several different links and opening them in new tabs. Now a function of my toolbar involves selecting text in the web page and then clicking a button to copy that text into the Toolbar. So let's do that in tab 3. We select text and click the button and nothing is there. However, if 开发者_JAVA百科we select text in tab 2, then go back to tab 3 and click the button we get the text selected in tab 2. So...the toolbar in tab 3 getting stuff from tab 2. Not good.

I have traced this problem back to static references inside our COM object, the toolbar.

[ComVisible(true), Guid("2CC75392-1182-470D-BECC-EFA33E629AB8")]
[CLSCompliant(false)]
public sealed class Toolbar : ADXIEToolbar
{

    public static Toolbar Instance;

    public Toolbar()
    {
        Instance = this;

        InitializeComponent();

    }

   ...other code...
}

Note only one toolbar instance exists per each IE tab.

This reference doesn't get assigned properly, almost like it isn't thread safe (it isn't) but instead not domain safe or something. It will sometimes reference another instance down the line. Same with other static fields and even thread-safe singletons. I don't get it.

Also note that if I pass a reference to this toolbar (inside InitializeComponent) to a control I have the same issue.

this.publicationDateCb.Toolbar = this;

This reference will sometimes point to a different tab.

If I use a purely subscription based model with absolutely zero static references with the toolbar as the referee then things seem to work fine. This basically means I would have to re-design the program to where no classes interacted with each other directly - they fire events that the toolbar subscribes to, calling methods in other classes. Ouch.

So should I go with that model (which may be ideal but I am pretty far along here) or is there a simple fix I am missing here?

Other notes:

  • All IE tabs are running in seperate processes.
  • The BHO/Toolbar is running in the same process as the IE tab.
  • I am using Add-In-Express for Internet Explorer to handle the IE integration.
  • The project is written for .NET 3.5; the loader uses .NET 2.0


If you want to share your selected text within all your toolbars you can look at: http://www.add-in-express.com/creating-addins-blog/2009/06/19/internet-explorer-plugin-settings-synchronize/


Problem solved but static references are gone. I did a few things:

First off, I changed the target .NET version to 4.0. Apparently BHOs written in 4.0 work better - I can't find a link to substantiate this claim but I have read it somewhere.

More importantly I did away with static references within the assembly altogether. I got rid of the singletons and instead created a property for each former singleton class in my Toolbar class, which will always be unique. I then passed a reference to the Toolbar whenever a class needed to reference a former singleton.

So...constructors look like this now:

internal class RegistryData
{
     public RegistryData(Toolbar toolbar)
     {
          ToolbarRef = toolbar;
     }

     ...
}

And let's say RegistryData needs to call Messaging.

private void RegistryUpdated(int keyId)
{
    ToolbarRef.Messaging.SendMessage(keyId);
}

Huge pain, right? Hours of work. But problem solved. I would not be shocked if this issue were related exclusively to Add-In-Express.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜