可能的线程/COM/UI 问题

发布于 2024-07-08 05:04:58 字数 1452 浏览 4 评论 0原文

我正在为 IE(6+) 编写一个工具栏。 中的各种示例栏

我使用了codeproject.com (http://www.codeproject. com/KB/dotnet/IE_toolbar.aspx),并且有一个可以工作的工具栏,注册注销等。我希望工具栏做的是当用户的鼠标移到该 div 上时突出显示 html 页面中的 div 。 到目前为止,突出显示代码有效,但我想在工具栏上的标签中显示 div 的名称(如果存在)(随着鼠标移动等而改变)。

我一生都无法让这种情况发生,尝试调试它是一场噩梦。 由于程序集托管在 IE 中,我怀疑我通过尝试从未创建该控件的线程更新标签上的文本而导致异常(在 IE 中),但因为该异常发生在 IE 中,所以我没有看到它。

解决方案是尝试使用 Invoke 以线程安全的方式更新控件吗? 如果是这样怎么办?

这是事件代码:

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;
}

这是对工具栏进行线程安全更新的尝试:

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 });
     }
}

非常感谢任何建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮光沉寂 2024-07-15 05:04:58

我无法真正重现该问题(为 IE 工具栏创建测试项目有点太多了),但您可以尝试以下操作:

将以下例程添加到公共静态(扩展方法)类:

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

然后替换该部分第一个块中的类似代码如下:

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

这是避免 UI 应用程序中线程安全问题的可靠方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文