我试图在同一个IE9会话中打开多个网页。似乎我在打开第一个网址之前必须等待一段时间,然后才能打开其余的网址,但使用Thread.Sleep看起来似乎很骇人听闻。有什么办法可以做得更好吗?在IE9中使用流程打开多个选项卡
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("[email protected]!");
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)。任何更少的,它有时只是把一部分的网站up ......不...从头开始,这只有在IE会话已经存在时才有效。进程退出是因为它将url移交给了现有的IE会话。
不工作,如果你做了Thread.Sleep任何短于约1000毫秒,所以我认为它仍然没有做正确的事情。 (如果目标计算机真的很慢?) – tofutim 2011-06-01 19:03:09
那么我怀疑你的程序没有用户界面? – 2011-06-01 19:09:24
本程序是否具有UI有什么区别? – tofutim 2011-06-01 19:13:33