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