开发者

Open multiple-tabs in IE9 using Process

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
          开发者_如何学C  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);
                        }
                    }


Not exactly what you asked for, but i found this

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜