InternetExplorer的自动化对象+的PerformanceCounter =不工作? [英] InternetExplorer automation object + PerformanceCounter = not working?

查看:151
本文介绍了InternetExplorer的自动化对象+的PerformanceCounter =不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在试图建立一个简单的组件,它应该监视,如果用户打开一个窗口,一个特定的URL(仅适用于IE)。
所以我写了这个组件,一切工作正常,所以我的应用程序,它需要整合的。
问题是,在本申请中PerformanceCounters被使用,并且这些似乎干扰InternetExplorer的自动化对象的行为

I'm currently trying to build a simple component which should monitor, if the user opens a window with a specific URL (IE only). So I wrote this component and everything works fine, so I integrated it with the application, where it was needed. The problem is, in this application PerformanceCounters are used, and these seem to disturb the behaviour of the InternetExplorer automation object.

所以我写了这个小样本来演示问题:

So I wrote this little sample to demonstrate the problem:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
using SHDocVw;

namespace IEHookTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t = new Thread(PerfCounter);
            t.Start();

            URLInterceptor.Instance.StartListening();
            Console.ReadLine();
        }

        private static void PerfCounter()
        {
            PerformanceCounter cpuPc = 
                new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
            do
            {
                float x = cpuPc.NextValue();
                System.Diagnostics.Debug.WriteLine(x);
                Thread.Sleep(50);
            } while (true);
        }
    }

    class URLInterceptor
    {
        private const string DEMANDED_URL = "test";
        private ShellWindows _shellWindows = new ShellWindows();

        public static readonly URLInterceptor Instance = new URLInterceptor();
        private static int _count = 0;
        private URLInterceptor()
        {
        }

        public void StartListening()
        {
            _count++;
            _shellWindows.WindowRegistered -= ShellWindows_WindowRegistered;
            _shellWindows.WindowRegistered += new DShellWindowsEvents_WindowRegisteredEventHandler(ShellWindows_WindowRegistered);
            FindIEs();
        }

        private void FindIEs()
        {
            int count = 0;
            foreach (InternetExplorer browser in _shellWindows)
            {
                browser.NewWindow3 -= browser_NewWindow3;
                browser.NewWindow3 += new DWebBrowserEvents2_NewWindow3EventHandler(browser_NewWindow3);
                count++;
            }
        }

        private void ShellWindows_WindowRegistered(int lCookie)
        {
            FindIEs();
        }

        private void browser_NewWindow3(ref object ppDisp,
                                        ref bool Cancel,
                                        uint dwFlags,
                                        string bstrUrlContext,
                                        string bstrUrl)
        {
            if (!string.IsNullOrEmpty(bstrUrl) && bstrUrl.ToLower().Contains(DEMANDED_URL))
            {
                Cancel = true;
                Console.WriteLine("catched URL: " + bstrUrl);
            }
        }
    }
}

此示例需要在Microsoft Internet控制(SHDOCVW)的引用。
为了测试样品,只需打开谷歌和搜索测试。就拿第一个链接,并在新标签或窗口中打开它。
你会看到,有时NewWindow3事件引发的,有时不是。
但是,如果你注释掉行15(螺纹开始),按预期工作的对象,并提出了为每一个新的窗口事件。

This sample needs a reference to the "Microsoft Internet Controls" (SHDocVw). To test the sample, just open google and search for "test". Take the first link and open it in a new tab or window. You will see, that sometimes the "NewWindow3" event is raised, and sometimes it is not. But if you comment out line 15 (Thread start), the objects works as expected, and raises the event for every new window.

所以我的问题是,为什么是性能计数器扰乱InternetExplorer对象,我怎么可以同时使用。我试图运行在一个新的AppDomain显示器组件,但没有解决问题。只有创建一个新的进程是一个解决方案,但是这是要我不想做,有几个原因。

So my question is, why are is the performance counter disturbing the InternetExplorer object, and how can I use both. I tried to run the monitor component in a new AppDomain, but that didn't solve the problem. Only creating a new process was a solution, but this is want I don't want to do, for several reasons.

我测试WIN2K3服务器上用IE 7。

I'm testing on Win2k3 server with IE 7.

推荐答案

要保持一个ArrayList引用解决问题。似乎ShellWindows集合不成立的引用。

To keep the references in an Arraylist solves the problem. Seems that the ShellWindows Collection doesn't hold the references.

这篇关于InternetExplorer的自动化对象+的PerformanceCounter =不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆