【发布时间】:2013-08-19 06:48:12
【问题描述】:
我无法理解我正在构建的这个函数的流程。
public void PortalLogin(AutoResetEvent signal)
{
// Navigate to portal
string portalUrl = "website_name";
string portalEmail = "email@email.com";
string portalPassword = "password";
Action action2 = () =>
{
webBrowser2.Tag = signal;
webBrowser2.Navigate(portalUrl);
webBrowser2.DocumentCompleted -= WebBrowserDocumentCompleted;
webBrowser2.DocumentCompleted += WebBrowserDocumentCompleted;
};
webBrowser2.Invoke(action2);
signal.WaitOne();
// Login to O365 portal
webBrowser2.Invoke(new Action(() =>
{
HtmlElement head = webBrowser2.Document.GetElementsByTagName("head")[0];
HtmlElement testScript = webBrowser2.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)testScript.DomElement;
element.text = "function PortalLogin() { document.getElementById('userid').value = '" + portalEmail + "'; document.getElementById('password').value = '" + portalPassword + "'; document.getElementById('login').submit(); }";
head.AppendChild(testScript);
webBrowser2.Document.InvokeScript("PortalLogin");
}));
}
... more functions after this
当我单步执行它时,它似乎没有“及时”调用脚本的 document.getElementById('login').submit(); 部分。在InvokeScript完全完成之前,我如何确保没有任何事情发生?
此外,如果您看到任何多余的代码或可以清理的东西,那也很棒。
编辑:这里是 DocumentCompleted 函数。
private void WebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs Url)
{
((AutoResetEvent)((WebBrowser)sender).Tag).Set();
}
【问题讨论】:
-
显示您的
WebBrowserDocumentCompleted。我假设你从那里提高signal?另外,PortalLogin是否在单独的线程上运行(来自webBrowser2的父线程)? -
是的,它在单独的线程上运行。
标签: c# javascript forms browser webbrowser-control