有人可以帮我解决吗?我有2种类型的错误信息
代码是:
private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
{
HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
string text = this.textBox5.Text;
htmlDocument.LoadHtml(text);
HtmlNodeCollection htmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//img/@alt");
int num1 = 0;
int k = 0;
if (htmlNodeCollection == null || this.backgroundWorker3.CancellationPending)
return;
string links = "";
foreach (HtmlNode htmlNode in (IEnumerable<HtmlNode>) htmlNodeCollection)
{
HtmlNode aTag = htmlNode;
int num2 = int.Parse(this.textBox7.Text);
this._busy.WaitOne(-1);
if (!this.backgroundWorker3.CancellationPending)
{
++k;
this.Invoke((Delegate) (() => this.richTextBox4.AppendText(k.ToString() + "." + Environment.NewLine + aTag.InnerHtml + aTag.Attributes["alt"].Value + Environment.NewLine + Environment.NewLine)));
++num1;
}
this.Invoke((Delegate) (closure_0 ?? (closure_0 = (Action) (() => links = this.richTextBox4.Text + Environment.NewLine))));
System.IO.File.WriteAllText(this.textBox2.Text + "/Descriptions.txt", links);
if (num1 == num2)
{
this.backgroundWorker3.CancelAsync();
if (!this.backgroundWorker3.CancellationPending)
this._busy.Reset();
}
}
}
谢谢
这行是无用的代码:
this.Invoke((Delegate) (closure_0 ?? (closure_0 = (Action) (() => links = this.richTextBox4.Text + Environment.NewLine))));
您可以安全地将其替换为:
this.Invoke(new Action(() => { links = this.richTextBox4.Text + Environment.NewLine; }));
您的第一个问题是在您显示的代码中没有名为closure_0
的变量或类字段。
其次,您在UI上创建和调用操作的语法是复杂和错误的,使用上面更简单的方法。