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.

Recommended Answers

All 3 Replies

>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.

Please show the code where you do the invoke.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.