-2

i was facing the exception Cross-thread error on my methods (void) i solved them by :

public delegate void onitemAdd(ListViewItem client);
public void OnItemAdd(ListViewItem itemtoadd)
{
    if (this.InvokeRequired)
    {
        onitemAdd adder = new onitemAdd(OnItemAdd);
        this.Invoke(adder, new object[] { itemtoadd });
    }
    else
    {
        if (itemtoadd != null)
        ClientsView.Items.Add(itemtoadd);
    }
}

but i don't know how can i do the same but to return the value returned in :

public delegate ListViewItem ItemReturn(Client client);
public ListViewItem OnReturnitem(Client client)
{
    if (this.InvokeRequired)
    {
        ItemReturn itm = new Zserver.Form1.ItemReturn(OnReturnitem);
        this.Invoke(itm, new object[] { client });
        // need to return a value of the invoked method 
    }
    else
    {
        ListViewItem item = ClientsView.Items
         .Cast<ListViewItem>()
         .FirstOrDefault(x => x.SubItems[1].Text == client.IPadress);
        return item;
    }
}
Huster
  • 329
  • 2
  • 3
  • 12

1 Answers1

1

Solved by :

object ob = this.Invoke(itm, new object[] { client });
return ob as ListViewItem;
Huster
  • 329
  • 2
  • 3
  • 12