I have two functions:

int GetRowCount()
return lvComponent.Items.Count;

string GetCellText(int row, int col)
return lvComponent.Items[row].SubItems[col].Text;

lvComponent is a ListView control.

I call both of them from another thread using Invoke.

The first one works perfectly.

The second one throws an exception saying that you can't access controls from a different thread.

How come the first one works and the second doesn't. The way I call Invoke follows the standard method described in numerous web articles.

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

7 Years
Discussion Span
Last Post by nick.crane

>I call both of them from another thread using Invoke.

It's illegal to call a method or property on a control from any thread other than the one that created it. All cross-thread calls must be explicitly marshalled to the thread that created the control, using the Control. Use Invoke or Control.BeginInvoke method.


As mentioned in my original post, I AM using Invoke to call both routines. Inspite of this the second call fails, even when using Invoke. I get the Cross-Thread error message.

Edited by weavercs: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.