使用Process在IE9中打开多个选项卡

时间:2011-06-01 18:47:04

标签: c# internet-explorer-9

我正在尝试在同一个IE9会话中打开多个网页。看来我打开第一个url后我必须等待一段时间才能打开剩下的部分,但是使用Thread.Sleep似乎是hackish。有什么方法可以做得更好吗?

foreach (string url in urls)
{
    try
    {
        if (!isLaunched)
        {
            Process p = new Process();
            p.StartInfo.FileName = browser;
            p.StartInfo.Arguments = url;
            p.Start();

            Thread.Sleep(1000); // somewhere between 500 and 1000
            isLaunched = true;
        }
        else
        {
            Process.Start(url);
        }
    }
}

注意:如果我没有等待时间,我最终会在一个页面上进行一次浏览器会话(最后一页。

更新 - 我尝试了p.WaitForInputIdle()但它没有用。 - 每http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/fe4be58f-9c9d-4e52-8ced-e3cd9c1bbee7我也试过

int i = 0;
    while (i == 0)
    {
        Console.WriteLine("!$@!");
        Thread.Sleep(100);
        p.Refresh();
        i = p.MainWindowHandle.ToInt32();
    }

但收到了错误:

Process has exited, so the requested information is not available.
   at System.Diagnostics.Process.EnsureState(State state)
   at System.Diagnostics.Process.get_MainWindowHandle()

UPDATE2: 下面的代码工作(等待IE9启动退出),但仍然需要Thread.Sleep(100)。任何更少,它有时只会放置一部分网站......不......划痕,这只有在已经有IE会话时才有效。进程退出是因为它将URL移交给现有的IE会话。

                foreach (string url in urls)
                {
                    try
                    {
                        if (!isLaunched)
                        {
                            Process p = new Process();
                            p.StartInfo.FileName = browser;
                            p.StartInfo.Arguments = url;
                            p.Start();
                            p.WaitForExit();
                            Thread.Sleep(100);
                            isLaunched = true;
                        }
                        else
                        {
                            Process.Start(url);
                        }
                    }

2 个答案:

答案 0 :(得分:0)

不完全是你要求的,但我找到了this

ie command line switches似乎不支持启动标签:/

答案 1 :(得分:0)

foreach (string url in urls)
{
    try
    {
        if (!isLaunched)
        {
            Process p = new Process();
            p.StartInfo.FileName = browser;
            p.StartInfo.Arguments = url;
            p.Start();

            if(!p.WaitForInputIdle()){ //Should wait indefinitely until the 
                                       //process is idle
              Thread.Sleep(1000);// Just in case the process has no message loop
            }

            isLaunched = true;
        }
        else
        {
            Process.Start(url);
        }
    }
}