我正在尝试自动化ie.This是我的代码来捕捉窗口
ProcessStartInfo psi = new ProcessStartInfo();
psi.CreateNoWindow = false;
psi.FileName = "IExplore.exe";
psi.Arguments = "-nomerge about:blank ";
psi.WindowStyle = ProcessWindowStyle.Normal;
Process p = new Process();
p.StartInfo = psi;
if (p.Start())
{
int maxWait = 10000, wait = 0;
while (!p.HasExited && (p.MainWindowHandle == IntPtr.Zero))
{
wait += 10;
Thread.Sleep(10);
p.Refresh();
if (wait > maxWait) break;
}
wait = 0;
while (!p.HasExited && (_IE == null))
{
_IE = null;
ShellWindows m_IEFoundBrowsers = new ShellWindowsClass();//here i get exception
foreach (InternetExplorer Browser in m_IEFoundBrowsers)
{
if (Browser.HWND == (int)p.MainWindowHandle)
{
_IE = Browser;
break;
}
}
if ((_IE != null) || (wait > maxWait)) break;
else
{
wait += 10;
Thread.Sleep(10);
}
}
if (_IE != null)
{
IE.Visible = true;
IE.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(IE_DocumentComplete);
}
else
{
Console.WriteLine("Problem opening IE!");
}
}
这段代码工作正常,但是当我尝试通过remoteapp启动应用程序时,我得到了异常,我猜是理由是一些访问相关但不确定要做什么。请帮忙
答案 0 :(得分:1)
最后让它工作只需用小的替换上面的大代码
**
_IE = new InternetExplorer();
IE.Visible = true;
IE.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(IE_DocumentComplete);
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
**
但是,如果自动化失败并且因为卡住然后休息所有自动化将开始抛出此异常,我也会遇到异常。对此的解决方案是我需要关闭失败的实例,即从任务管理器,然后所有将再次正常工作。