只有赋值,调用,递增,递减和新对象表达式才能用作语句多线程调用

时间:2014-01-21 16:20:12

标签: c# multithreading objectlistview

我想从objectlistview

获取subitem [7] .text

形成另一个线程。 错误:

  

仅分配,调用,增量,减量和新对象   表达式可以用作语句

有问题的一行:

string url = objectListView1.Invoke(new Action(() => 
objectListView1.SelectedItems[objectListView1.SelectedIndex].SubItems[7].Text));

C#代码:

void getThumbnail()
{
    try
    {
        HtmlWeb web = new HtmlWeb();

        string url = objectListView1.Invoke(new Action(() => 
        objectListView1.SelectedItems[objectListView1.SelectedIndex].SubItems[7].Text));
        HtmlAgilityPack.HtmlDocument htmldoc = web.Load(url);
        htmldoc.OptionFixNestedTags = true;

        var link = htmldoc.DocumentNode.SelectSingleNode("//link[@itemprop='thumbnailUrl']");
        var href = link.Attributes["href"].Value;
        pictureBox1.Invoke(new Action(() => pictureBox1.Load(href)));
    }
    catch (HtmlAgilityPack.HtmlWebException)
    {

    }
}
private void objectListView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
    Thread thr = new Thread(() => getThumbnail());
    thr.Start();
}

1 个答案:

答案 0 :(得分:1)

string url = objectListView1.Invoke(new Action(() => 
       objectListView1.
       SelectedItems[objectListView1.SelectedIndex].
       SubItems[7].Text));

Action委托不会返回任何结果。如果您想返回string,则需要使用Func<string>

string url = objectListView1.Invoke(new Func<string>(() => 
       objectListView1.
       SelectedItems[objectListView1.SelectedIndex].
       SubItems[7].Text)) as string;