使用Process在IE9中打开多个选项卡 [英] Open multiple-tabs in IE9 using Process

查看:294
本文介绍了使用Process在IE9中打开多个选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在同一IE9会话中打开多个网页.看来我在打开第一个URL之后必须等待一些时间才能打开其余的URL,但是使用Thread.Sleep似乎有点黑.有什么办法可以做得更好?

I'm trying to open multiple-webpages in the same IE9 session. It seems that I have to wait a bit of time after opening the first url before I can open the rest, but using Thread.Sleep seems hackish. Is there any way I can do better?

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

注意:如果我不等时间,我将结束一个浏览器会话,并显示一页(最后一页).

Note: If I do not wait the time, I will end up with one browser session with one page (the last page.

更新: -我尝试了p.WaitForInputIdle(),但没有成功. -每个 http://social .msdn.microsoft.com/Forums/zh-CN/winforms/thread/fe4be58f-9c9d-4e52-8ced-e3cd9c1bbee7 我也尝试过

Update: - I tried p.WaitForInputIdle() but it did not work. - Per http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/fe4be58f-9c9d-4e52-8ced-e3cd9c1bbee7 I also tried

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会话.

Update2: The code below works (waits for IE9 launch to exit), but still requires Thread.Sleep(100). Any less and it sometimes only puts a portion of the sites up... No... scratch that, this only works if there is an IE session already. The Process exits because it hands off the url to the existing IE session.

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

推荐答案

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

这篇关于使用Process在IE9中打开多个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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