Only assignment, call, increment, decrement, and new object expressions can be used as a statement multithreading invoke

StackOverflow https://stackoverflow.com/questions/21263561

Frage

I want to get subitem[7].text from objectlistview

form another thread. Error:

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Problematic line:

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

C# code:

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();
}
War es hilfreich?

Lösung

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

Action delegate doesn't return any result. If you want to return string, then you need to use Func<string>:

string url = objectListView1.Invoke(new Func<string>(() => 
       objectListView1.
       SelectedItems[objectListView1.SelectedIndex].
       SubItems[7].Text)) as string;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top