Open multiple-tabs in IE9 using Process

无人久伴 提交于 2019-12-11 05:12:53

问题


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.

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

but got an error:

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

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

回答1:


Not exactly what you asked for, but i found this

The command line switches for ie doesnt seem to support launching tabs :/




回答2:


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


来源:https://stackoverflow.com/questions/6206149/open-multiple-tabs-in-ie9-using-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!