本文整理汇总了C#中System.Windows.Forms.WebBrowser.Invoke方法的典型用法代码示例。如果您正苦于以下问题:C# WebBrowser.Invoke方法的具体用法?C# WebBrowser.Invoke怎么用?C# WebBrowser.Invoke使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.WebBrowser
的用法示例。
在下文中一共展示了WebBrowser.Invoke方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: setWbDocumentText
private void setWbDocumentText(WebBrowser wb, string str)
{
if (wb.InvokeRequired)
{
wb.Invoke(setWbDT, wb, str);
}
else {
wb.DocumentText = str;
}
}
示例2: Navigate
public string Navigate(string url, int timeout)
{
string gethtml = string.Empty;
try
{
int interval = 500;
using (WebBrowser browser = new WebBrowser())
{
browser.ScriptErrorsSuppressed = false;
DateTime startTime = DateTime.Now;
bool isbusy = true;
int length = 0;
//browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
//browser.Navigating += new WebBrowserNavigatingEventHandler(browser_Navigating);
browser.Navigate(url);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " Still Loading");
}
while (hitCount < 10)
{
double t = Math.Ceiling((DateTime.Now - startTime).TotalSeconds);
if (t >= timeout)
{
throw new Exception("Visiting about new exception delay, since the setting is timeout");
}
BrowserEventHandler browserEventHanler = delegate() { isbusy = !browser.IsBusy; };
browser.Invoke(browserEventHanler);
if (browser.Document.All["flight-info"] != null)
{
int len = 0;
if (!string.IsNullOrEmpty(browser.Document.All["flight-info"].InnerHtml))
len = browser.Document.All["flight-info"].InnerHtml.Length;
if (len == length)
{
hitCount++;
}
else
{
hitCount = 0; length = len;
}
//System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "->flight-info-:InnerHtml" + browser.Document.All["flight-info"].InnerHtml);
}
if (!string.IsNullOrEmpty(browser.Document.All["flight-info"].InnerHtml))
length = browser.Document.All["flight-info"].InnerHtml.Length;
Application.DoEvents();
System.Threading.Thread.Sleep(interval);
}
if (browser.Document.All["flight-info"] != null)
{
System.Diagnostics.Debug.Write("=".PadLeft(50, '='));
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "->flight-info-:InnerHtml" + browser.Document.All["flight-info"].InnerHtml);
gethtml = browser.Document.All["flight-info"].InnerHtml;
}
//var htmldocument = (mshtml.HTMLDocument)browser.Document.DomDocument; System.Diagnostics.Debug.Write("=".PadLeft(50, '='));
//System.Diagnostics.Debug.WriteLine(htmldocument.documentElement.outerHTML); System.Diagnostics.Debug.Write("=".PadLeft(50, '='));
//System.Diagnostics.Debug.WriteLine(browser.Document.Body.OuterHtml);
}
}
catch (Exception ex)
{
Log.LogErr(ex);
}
return gethtml;
}
示例3: Navigate
public void Navigate()
{
try
{
this.TextAsync = string.Empty;
this.HTMLSourceCode = string.Empty;
int interval = 500;
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.ScriptErrorsSuppressed = false;
browser.ScrollBarsEnabled = false;
browser.AllowNavigation = true;
DateTime startTime = DateTime.Now;
bool isbusy = true;
bool isRefresh = false;
// bool completed = false;
int count = 6;
int index = 0;
int length = 0;
browser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
do
{
//browser.DocumentCompleted += delegate(object sender, WebBrowserDocumentCompletedEventArgs e)
//{
// this.HTMLSourceCode = browser.Document.Body.OuterHtml;
// string url0 = browser.Document.Url.ToString();
// completed = url0.Equals(e.Url.ToString());
//};
if (!isRefresh)
browser.Navigate(url);
while (browser.ReadyState != WebBrowserReadyState.Complete || !browser.StatusText.Equals("Done"))
System.Windows.Forms.Application.DoEvents();
if (this.completed)
this.TextAsync = browser.Document.Body.OuterHtml;
string strRegex = "(?<ITEM><figure class=\"flight-info none\" id=\"flight-info\" style=\"display: block;\">)[\\S\\s]*?(?=</figure>)";
Regex re = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
Match mc = re.Match(this.TextAsync);
if (mc.Length > 0)
{
System.Diagnostics.Debug.WriteLine(mc.Value);
break;
}
else
isRefresh = true;
if (browser.ReadyState == WebBrowserReadyState.Complete && browser.StatusText.Equals("Done"))
{
System.Windows.Forms.Application.DoEvents();
BrowserEventHandler browserEventHanler = delegate() { isbusy = !browser.IsBusy; };
browser.Invoke(browserEventHanler);
//try
//{
// browser.Document.InvokeScript("flight.submit()");
// isRefresh = true;
// completed = false;
//}
//catch (Exception ex)
//{
// System.Diagnostics.Debug.Write(ex);
//}
}
if (!isbusy)
{
int len = browser.Document.Body.OuterHtml.Length;
if (len == length) { index++; }
else { index = 0; length = len; }
if (index == count) { isbusy = false; }
}
if (this.completed)
length = browser.Document.Body.OuterHtml.Length;
System.Threading.Thread.Sleep(interval);
double t = Math.Ceiling((DateTime.Now - startTime).TotalSeconds);
if (t >= this.timeout)
{
this.Error = new Exception("Visiting about new exception delay, since the setting is timeout");
break;
}
} while (isbusy);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
catch (Exception ex) { throw ex; }
}
示例4: Navigate
public string Navigate(string url, int timeout, string flightHtmlElementID)
{
string gethtml = string.Empty;
try
{
int interval = 500;
using (WebBrowser browser = new WebBrowser())
{
browser.ScriptErrorsSuppressed = false;
DateTime startTime = DateTime.Now;
bool isbusy = true;
int length = 0;
browser.Navigate(url);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " Still Loading");
System.Threading.Thread.Sleep(interval);
double t = Math.Ceiling((DateTime.Now - startTime).TotalSeconds);
if (t >= timeout)
{
throw new Exception("Visiting about new exception delay, since the setting is timeout");
}
}
while (hitCount < 4)
{
double t = Math.Ceiling((DateTime.Now - startTime).TotalSeconds);
if (t >= timeout)
{
throw new Exception("Visiting about new exception delay, since the setting is timeout");
}
BrowserEventHandler browserEventHanler = delegate() { isbusy = !browser.IsBusy; };
browser.Invoke(browserEventHanler);
if (browser.Document.All[flightHtmlElementID] != null)
{
int len = 0;
if (!string.IsNullOrEmpty(browser.Document.All[flightHtmlElementID].InnerHtml))
len = browser.Document.All[flightHtmlElementID].InnerHtml.Length;
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss") + string.Format(" hitCnt:{0};len:{1};length:{2}", hitCount, len, length));
if (len == length)
{
hitCount++;
}
else
{
hitCount = 0; length = len;
}
}
if (!string.IsNullOrEmpty(browser.Document.All[flightHtmlElementID].InnerHtml))
length = browser.Document.All[flightHtmlElementID].InnerHtml.Length;
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " begin DoEvents and Sleep");
//Application.DoEvents();
System.Threading.Thread.Sleep(interval);
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " end DoEvents and Sleep");
}
if (browser.Document.All[flightHtmlElementID] != null)
{
gethtml = browser.Document.All[flightHtmlElementID].InnerHtml;
}
}
}
catch (Exception ex)
{
Log.LogErr(ex);
}
return gethtml;
}
示例5: WaitForCondition
public static object WaitForCondition(WebBrowser browser, Func<object> check_condition, int mss)
{
if (browser.InvokeRequired)
return browser.Invoke(new delegateWaitForCondition(WaitForCondition), browser, check_condition, mss);
DateTime timeout = DateTime.Now.AddMilliseconds(mss);
while(DateTime.Now < timeout)
{
if (browser.Document != null && browser.Document.Body != null)
{
object o = check_condition();
if (o != null)
return o;
}
ThreadRoutines.Wait(20);
}
return null;
}