Does reading SelectedIndex on a combo box require an invoke?

A

Adam Benson

Hi,

If you're not on the UI thread - responding to a network event, say - and
you want to read the SelectedIndex of a combo box, do you have to do an
Invoke? Or is it just a simple data read with no windows messages involved?

The reason I ask is that in some of my handlers if the SelectedIndex is -1,
then I can return from the event handler straight away with no need for any
further work. Doing an Invoke just for that seems a bit long-winded.

Ta,

Adam.

==============================
(e-mail address removed)
 
M

Marc Gravell

Perhaps keep a volatile or sync-locked field somewhere, and update it
on the UI thread when the SelectedIndexChanged event fires; then in
your non-UI thread can just check this field, without having to go to
the UI thread.

Marc
 
P

Peter Duniho

If you're not on the UI thread - responding to a network event, say - and
you want to read the SelectedIndex of a combo box, do you have to do an
Invoke? Or is it just a simple data read with no windows messages
involved?

You have to do an Invoke() to use the property. You can verify this
yourself by writing the code without the Invoke() and seeing what
happens. Assuming you haven't set the CheckForIllegalCrossThreadCalls
property to false, you'll generate a cross-thread exception.
The reason I ask is that in some of my handlers if the SelectedIndex is
-1,
then I can return from the event handler straight away with no need for
any
further work. Doing an Invoke just for that seems a bit long-winded.

Marc's suggestion is a good one for what you're asking. In fact, given
the need you're describing, you might well just need a volatile flag that
you set in the SelectedIndexChanged handler. True if something's
selected, false if not, and then in your other event handler(s) just
return right away if the flag is false.

But to complete the answer: yes, an Invoke() is required if you want to
retrieve the current SelectedIndex value directly from the control.
"Under the hood", using that property goes through the unmanaged control
class window procedure, and that has to be done via Control.Invoke().

Pete
 
A

Adam Benson

Thanks, Marc and Peter.

I suspected as much but wanted to be sure - I'm currently having to work
under VS.NET 2003 so I don't get cross thread exceptions, just a lock up
when you least expect it!

- AB
======
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top