0
我有WebBrowser控件,並且我正在調用一個函數來獲取線程中的所有控件ID。這些函數在我從UI線程調用它時起作用,但如果從一個單獨的線程調用它,則不會得到任何控制權。WebBrowser控件獲取線程上的所有控件無法正常工作
我迷路了,請幫
電話:
List<WebOparator.WebOparator.WebControls> wcList = oparator.GetAllControlName();
int tryCount = 0;
do
{
if (wcList.Count == 0)
{
tryCount++;
Thread.Sleep(2000);
wcList = oparator.GetAllControlName();
}
if (tryCount >= 5) break;
} while (wcList.Count == 0);
方法:
public List<WebControls> GetAllControlName()
{
List<WebControls> names = new List<WebControls>();
if (this.InvokeRequired)
{
this.BeginInvoke((MethodInvoker)delegate
{
int i = 0;
foreach (HtmlElement element in myBrowser1.Document.All)
{
if (element.Id != null)
{
i++;
names.Add(new WebControls() { sl = i, ID = element.Id, TagName = element.TagName });
}
}
});
}
else
{
int i = 0;
foreach (HtmlElement element in myBrowser1.Document.All)
{
if (element.Id != null)
{
i++;
names.Add(new WebControls() { sl = i, ID = element.Id, TagName = element.TagName });
}
}
}
return names;
}
編輯:
我發現功能,通過所有的控制權就轉至環路和將它們添加到列表中,但返回一個空列表....
不要這樣做。改用HTML Agility Pack。 – SLaks
寫作'catch {throw; }' – SLaks