C# IE BHO:如何将 DOM 对象编组到工作线程?

发布于 2024-09-07 12:06:57 字数 368 浏览 4 评论 0原文

我正在尝试用 C# 编写一个 浏览器帮助程序对象 (BHO),用于在单独的线程。我已经看到了与此相关的其他几个问题,答案似乎是“您需要将 DOM 对象从创建它们的线程编组到您的工作线程。”很好的建议,而且非常有道理,但我找不到关于如何执行此操作的 C# 示例。有一些指向需要使用的 P/Invoke API 的模糊指针,但我很难了解如何将其实现到 BHO 中。

我通过示例学习效果最好,但遗憾的是,文档中缺乏此类内容的 .NET 示例。有人可以给我举一个例子,在托管代码 BHO 的上下文中,DOM 通过工作线程进行操作吗?

I am trying to write a Browser Helper Object (BHO) in C# that manipulates the DOM on a separate thread. I've seen several other questions related to this, and the answer seems to be "you need to marshal the DOM objects from the thread they were created on to your worker thread." Good advice, and it makes perfect sense, but I can find no C# examples on how to do this. There are some vague pointers to some P/Invoke APIs that need to be used, but I'm having difficulty seeing how to implement that into a BHO.

I learn best by example, and the documentation is woefully short of .NET examples of this sort of thing. Can someone point me to an example where, within the context of a managed code BHO, the DOM is manipulated via a worker thread?

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

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

发布评论

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

评论(1

土豪我们做朋友吧 2024-09-14 12:06:57

您不必执行任何手动编组;托管运行时代表您处理任何跨单元 COM 对象封送处理。

这是一个例子;此示例托管 BHO 一直等到 DocumentComplete 事件触发并启动 ThreadPool 后台线程,该线程等待一秒钟,然后将页面标题更改为“Hello, StackOverflow!”并添加一个带有特殊消息的新文本节点:

private void OnDocumentComplete(object frame, ref object urlObj)
{
    System.Threading.ThreadPool.QueueUserWorkItem((o) =>
    {
        System.Threading.Thread.Sleep(1000);
            HTMLDocument document = (HTMLDocument)this.browser.Document;
            document.title = "Hello, StackOverflow!";

            IHTMLDOMNode greetings = document.createTextNode("Hi there!");

            IHTMLDOMNode body = document.body as IHTMLDOMNode;
            body.insertBefore(greetings, body.firstChild);                
    }, this.browser);
}

#region IObjectWithSite Members

int IObjectWithSite.SetSite(object site)
{
    if (site != null)
    {
        this.browser = (WebBrowser)site;
        this.browser.DocumentComplete +=
         new DWebBrowserEvents2_DocumentCompleteEventHandler(
          this.OnDocumentComplete);
    }
    else
    {
        if (this.browser != null)
        {
            this.browser.DocumentComplete -=
             new DWebBrowserEvents2_DocumentCompleteEventHandler(
              this.OnDocumentComplete);
            this.browser = null;
        }
    }
    return 0;
}

int IObjectWithSite.GetSite(ref Guid guid, out IntPtr ppvSite)
{
    IntPtr punk = Marshal.GetIUnknownForObject(this.browser);
    int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
    Marshal.Release(punk);

    return hr;
}

#endregion

You shouldn't have to perform any manual marshaling; the managed runtime handles any cross-apartment COM object marshaling on your behalf.

Here's an example; this sample managed BHO waits until the DocumentComplete event fires and spins up a ThreadPool background thread that waits for a second then changes the title of the page to "Hello, StackOverflow!" and adds a new text node with a special message:

private void OnDocumentComplete(object frame, ref object urlObj)
{
    System.Threading.ThreadPool.QueueUserWorkItem((o) =>
    {
        System.Threading.Thread.Sleep(1000);
            HTMLDocument document = (HTMLDocument)this.browser.Document;
            document.title = "Hello, StackOverflow!";

            IHTMLDOMNode greetings = document.createTextNode("Hi there!");

            IHTMLDOMNode body = document.body as IHTMLDOMNode;
            body.insertBefore(greetings, body.firstChild);                
    }, this.browser);
}

#region IObjectWithSite Members

int IObjectWithSite.SetSite(object site)
{
    if (site != null)
    {
        this.browser = (WebBrowser)site;
        this.browser.DocumentComplete +=
         new DWebBrowserEvents2_DocumentCompleteEventHandler(
          this.OnDocumentComplete);
    }
    else
    {
        if (this.browser != null)
        {
            this.browser.DocumentComplete -=
             new DWebBrowserEvents2_DocumentCompleteEventHandler(
              this.OnDocumentComplete);
            this.browser = null;
        }
    }
    return 0;
}

int IObjectWithSite.GetSite(ref Guid guid, out IntPtr ppvSite)
{
    IntPtr punk = Marshal.GetIUnknownForObject(this.browser);
    int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
    Marshal.Release(punk);

    return hr;
}

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