我在Windows窗体项目中有一个WebBrowser控件代码。这个工作正常,如下所示。它首先导航到我的html页面,然后单击那里的锚标签。每个锚标记都将导航到一个网站。
现在,我尝试通过添加async / await使其异步。但这不起作用。知道这里遗漏的是什么吗?
注意:我无法更改e1.InvokeMember(“点击”);到webBrowser1.Navigate()。因为在我的真实场景中会有javascript代码来处理导航。因此我需要调用InvokeMember()本身。
参考
C#代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentCompleted += ExerciseApp;
HomoePageNavigate();
//ProcessUrlsAsync(@"C:\Samples_L\MyTableTest.html").Start();
}
private Task ProcessUrlsAsync(string url)
{
return new Task(() =>
{
TaskAwaiter<string> awaiter = ProcessUrlAsyncOperation(url);
string result = awaiter.GetResult();
MessageBox.Show(result);
});
}
// Awaiter inside
private TaskAwaiter<string> ProcessUrlAsyncOperation(string url)
{
TaskCompletionSource<string> taskCompletionSource = new TaskCompletionSource<string>();
var handler = new WebBrowserDocumentCompletedEventHandler((s, e) =>
{
// TODO: put custom processing of document right here
ExerciseApp(webBrowser1 , null);
taskCompletionSource.SetResult(e.Url + ": " + webBrowser1.Document.Title);
});
webBrowser1.DocumentCompleted += handler;
taskCompletionSource.Task.ContinueWith(s => { webBrowser1.DocumentCompleted -= handler; });
webBrowser1.Navigate(url);
return taskCompletionSource.Task.GetAwaiter();
}
private void HomoePageNavigate()
{
webBrowser1.Navigate(@"C:\Samples_L\MyTableTest.html");
}
List<string> visitedProducts = new List<string>();
private void ExerciseApp(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WriteLogFunction("A");
var wb = sender as WebBrowser;
int catalogElementIterationCounter = 0;
var elementsToConsider = wb.Document.All;
string productUrl = String.Empty;
bool isClicked = false;
foreach (HtmlElement e1 in elementsToConsider)
{
catalogElementIterationCounter++;
string x = e1.TagName;
String idStr = e1.GetAttribute("id");
if (!String.IsNullOrWhiteSpace(idStr))
{
//Each Product Navigation
if (idStr.Contains("catalogEntry_img"))
{
productUrl = e1.GetAttribute("href");
if (!visitedProducts.Contains(productUrl))
{
WriteLogFunction("B");
visitedProducts.Add(productUrl);
isClicked = true;
e1.InvokeMember("Click");
break;
}
}
}
}
if (visitedProducts.Count == 4)
{
visitedProducts = new List<string>();
}
if (!isClicked)
{
WriteLogFunction("C");
HomoePageNavigate();
//break;
}
}
private void WriteLogFunction(string strMessage)
{
using (StreamWriter w = File.AppendText("log.txt"))
{
w.WriteLine("\r\n{0} ..... {1} ", DateTime.Now.ToLongTimeString(), strMessage);
}
}
}
HTML
<html>
<head>
<style type="text/css">
table {
border: 2px solid blue;
}
td {
border: 1px solid teal;
}
</style>
</head>
<body>
<table id="four-grid">
<tr>
<td>
<a href="https://www.wikipedia.org/" id="catalogEntry_img63666">
<img src="ssss"
alt="B" width="70" />
</a>
</td>
<td>
<a href="http://www.keralatourism.org/" id="catalogEntry_img63667">
<img src="ssss"
alt="A" width="70" />
</a>
</td>
</tr>
<tr>
<td>
<a href="http://stackoverflow.com/users/696627/lijo" id="catalogEntry_img63664">
<img src="ssss"
alt="G" width="70" />
</a>
</td>
<td>
<a href="http://msdn.microsoft.com/en-US/#fbid=zgGLygxrE84" id="catalogEntry_img63665">
<img src="ssss"
alt="Y" width="70" />
</a>
</td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:2)
现在,我尝试通过添加async / await使其异步。但这是 不工作知道这里遗漏的是什么吗?
尽管存在问题标题,但您不会在代码中的任何位置使用async
或await
个关键字。因此,您可能无法理解它们的工作原理。显然,你正试图想出一个custom awaiter,但这也不是它的工作方式。
您可能需要查看this question,了解如何将WebBrowser.DocumentCompleted
事件转换为任务并等待它的示例。此外,async-await
wiki中列出的链接可以帮助您了解这一主题。
如果您需要更多有关如何使用async
/ await
和WebBrowser
任务的示例,包括点击自动化和网络报废,您可能会发现很多{{3} }}