race condition?

  • Thread starter Thread starter Berryl Hesh
  • Start date Start date
B

Berryl Hesh

I wouldn't nomally post this here, as it has something to do with the
ListView control usage I think, or maybe with a race condition or some
windoes messaging. I'm just not sure.

The test below succeeds IF i walk through the debugger and IF I examine the
ListViewItem's properties in the debugger visualizer. Otherwise, it fails.
Any ideas?

Thanks. BH

[Test]
public void SelectedItem_ReturnsFirstTheSelectectedItem() {
_addData();
Assert.That(_listView.Items.Count, Is.GreaterThan(0), "Data added
test");

var listViewItem = _listView.Items[0];
Assert.That(listViewItem, Is.Not.Null);
_listView.Focus();

listViewItem.Selected = true; ---------------XX only if in debug

Assert.That(_listView.SelectedItems.Count, Is.GreaterThan(0), "ListView
not responding to Selected=true");----xx---only if in debug

Assert.That(_widget.SelectedItem, Is.Not.Null, "Point of the test!");
}
 
I don't know the ListView control very well, at all which is part of the
problem, and I've already debugged this to the best of my current ability. I
was hoping by posting here I could get help in isolating the problem enough
to fix it, or find out I'm not using the ListView control correctly.

The test method I posted earlier is being invoked by a TestRunner, I assume.
I am not explicitly creating any other threads.

There class I am testing here is a wrapper around the ListView control
called ListViewLookupWrapper. It's code and inheritance chain is below. One
of the features of the class is to allow the SelectedIndexChanged event to
be delegated as nessary to some method outside of a windows form, where it
can be tested more easily (I have other tests showing that this scheme works
with and without a listView implementation). Another is to capture the
selected item as an ILookupDTO implementation - it is this SelectedItem
method that I'm trying to test. To do that I want to trigger the listview's
own SelectedIndexChanged event. I'm not doing anything with the event here
beyond trying to trigger it, just counting on the ListView to fulfill it's
contract and have the selected item available in it's own SelectedItems
property.

That's where I get to my previous post, as I can't yet prove or disprove
what's going on with the event firing in the debugger after I examine it,
but not otherwise.

Cheers, and thanks for sharing. BH

--------------------------------------------------------------------------------------

public class ListViewLookupWrapper : LookupListBase
{
protected readonly ListView _listView;
public ListViewLookupWrapper(ListView underlyingList) {
_listView = underlyingList;
_ listView.SelectedIndexChanged +=
OnSelectedIndexChanged; -<--XX----- the parent exposes this as a property
}

public override void Add(ILookupDTO lookupDto) {...}
public override void Clear() { _listView.Items.Clear(); }

public override ILookupDTO SelectedItem {
get {
if (_listView.SelectedItems.Count > 0) {
var listViewItem = _listView.SelectedItems[0];
var item = new
SimpleLookupDTO(listViewItem.SubItems[0].Text, listViewItem.Text);
return item;
}
return null;
}
}
}

public abstract class LookupListWrapperBase
{

public event EventHandler SelectedIndexChanged;

/// <summary>
/// exposed for inheritors to do testing
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnSelectedIndexChanged(object sender, EventArgs e) {
Console.WriteLine("Entering OnSelectedIndexChanged");
if (SelectedIndexChanged != null)
SelectedIndexChanged(sender, e);
}

public abstract ILookupDTO SelectedItem { get; }
public abstract void Add(ILookupDTO lookupDto);
public abstract void Clear();

}
 
Got it...

The problem was that the control had no Handle from my code, which turns out
to be as easy as using CreateControl(). The debugger must create a handle to
examine the control properties, which was forcing the SelectedItems property
to work.

That's (3) things I learned as a result of your 'bossy' response, so
thanks!! I still think you just don't like me though :-)
 
Back
Top