ListView Select Sometimes Fails

  • Thread starter Thread starter rlg
  • Start date Start date
R

rlg

We have C# 2005 multi-threaded app.

A background thread is processing data and periodically updating the
contents of a ListView. Sometimes, the user clicking on a ListView
item will not select the item (in the MouseClick event,
ListView.SelectedItems.Count = 0).

Initially we assumed it was because the background thread was in the
process of updating the ListView. However, breaking on the first line
of the MouseClick event reveals that the background thread is sleeping
(although perhaps with the timing of the debugger, the background
thread is still the culprit).

What could cause clicking on a ListView item to not select the item?

Thanks in advance...
 
Background threads shouldn't be updating UI controls. You should
delegate to the foreground thread in the case that it is the background
thread.


private delegate void UpdateItemHandler( );

private void MethodCalledFromEitherBackgroundOrUIThread( string data
) {
// Check if Method called from the non-UI thread
if( lv.InvokeRequired ) {
lv.Invoke( new UpdateItemHandler( UpdateItem ) );
}
else {
UpdateItem();
}
}

private void UpdateItem( ) {
lv.Items.Add( data );
....
}


Sincerely,
Bobby
 
Thanks for the input Bobby. We are in fact using BeginInvoke to update
the ListView control from the background data processing thread. Any
ideas why click select may not be working sometimes?
 
Sorry, without looking at the code and/or debugging, it is very hard to
determine the problem. If I were in this situation, I would start with
a separate dummy project and try to re-create the ListView control and
look at whether the selection is still giving the same behaviour.

Sincerely,
Bobby
 
Back
Top