rad*_*aku 4 c# delegates winforms
可能重复:
最简洁,最正确的避免交叉线程操作错误的方法?
我在运行程序时遇到错误.... {"跨线程操作无效:控制'listView1'从其创建的线程以外的线程访问."}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
TestObject argumentTest = e.Argument as TestObject;
string[] lines = argumentTest.ThreeValue.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
foreach (string vr in lines)
{
string country = argumentTest.OneValue.Trim();
string url = vr + country + '/code/' + argumentTest.TwoValue.Trim();
string sourceCode = WorkerClass.getSourceCode(url);
document.LoadHtml(sourceCode);
var title = document.DocumentNode.SelectSingleNode("//title");
var desc = document.DocumentNode.SelectSingleNode("//div[@class='productDescription']");
//-- eksekusi title
string isititle = title.InnerText;
string isititle2 = isititle.Replace("droidflashgame: ", "");
string isititle3 = Regex.Replace(isititle2, "[^A-Za-z0-9 ]+", "");
string isititle4 = isititle3.Substring(0, Math.Min(isititle3.Length, 120));
//-- Adding to list view for next step...
ListViewItem abg = new ListViewItem(isititle3);
abg.SubItems.Add(isititle4);
listView1.Items.Add(abg); // ERROR in Here?
Run Code Online (Sandbox Code Playgroud)
我在一些教程中说过使用invoke?但我尝试了很多但仍然有错误?
任何一只手?
试试这个.这对我来说很好
ListViewItem abg = new ListViewItem(isititle3);
if (listView1.InvokeRequired)
listView1.Invoke(new MethodInvoker(delegate
{
listView1.Items.Add(abg);
}));
else
listView1.Items.Add(abg);
Run Code Online (Sandbox Code Playgroud)