views:

93

answers:

1

Hello,

I am writing a toolbar for IE(6+). I have used the various sample bars from

codeproject.com (http://www.codeproject.com/KB/dotnet/IE_toolbar.aspx), and have a toolbar that works, registers unregisters etc. What I want the toolbar to do is to highlight divs within an html page as the users' mouse moves over that div. So far the highlighting code works, but I would like to display the name of the div (if it exists) in a label on the toolbar (changing as the mouse moves etc).

I cannot for the life of me get this to happen and trying to debug it is a nightmare. As the assembly is hosted in IE, I suspect that I am causing an exception (in IE) by trying to update the text on the label from a thread that didn't create that control, but because that exception is happening in IE, I don't see it.

Is the solution to try to update the control in a thread-safe way using Invoke? If so how?

Here is the event code:

private void Explorer_MouseOverEvent(mshtml.IHTMLEventObj e)
{
      mshtml.IHTMLDocument2 doc = this.Explorer.Document as IHTMLDocument2;
      element = doc.elementFromPoint(e.clientX, e.clientY);
      if (element.tagName.Equals("DIV", StringComparison.InvariantCultureIgnoreCase))
      {
          element.style.border = "thin solid blue;";
          if (element.className != null)
          {
               UpdateToolstrip(element.className);
          }
      } 
      e.returnValue = false;
}

and here is an attempt at thread-safe update of the toolbar:

delegate void UpdateToolstripDelegate(string text);

public void UpdateToolstrip(string text)
{
     if (this.toolStripLabel1.InvokeRequired == false)
     {
         this.toolStripLabel1.Text = text;
     }
     else
     {
         this.Invoke(new UpdateToolstripDelegate(UpdateToolstrip), new object[] { text });
     }
}

Any suggestions much appreciated.

+1  A: 

I can't really reproduce the issue (creating a test project for an IE toolbar is a tad too much work), but you can try this:

Add the following routine to a public static (extensions methods) class:

public static void Invoke(this Control control, MethodInvoker methodInvoker)
{
    if (control.InvokeRequired)
        control.Invoke(methodInvoker);
    else
        methodInvoker();
}

And then replace the section of similar code in the first block with this:

if (element.className != null)
{
    this.Invoke(() => toolStripLabel1.Text = element.className);
}

This is a sure-fire way of avoiding thread-safe issues in UI applications.

Darksider