I am using a generic ListView sorter implementing the IComparer interface.
Is this working on a separate thread from the main thread?
I have some funky results. It Sorts a static ListView just fine, however, once it gets populated with streaming data (it subscribes to some events that constantly add items to it) comparing fails and it becomes funky.
If its on a separate thread – any ideas on how should I modify it so it doesn’t interfere with the populating results (or vice-versa)?
Or if it IS on the same threads, any ideas on why this is happening?
Below is the code for update method that updates the listView (lstTrades)
EDIT: I PASTED THE WRONG CODE ORIGINALLY!!
private void UpdateList(foo t)
{
lstTrades.Items.Add(t.a);
int i = lstTrades.Items.Count - 1;
lstTrades.Items[i].SubItems.Add(t.b);
lstTrades.Items[i].SubItems.Add(t.c.ToString());
lstTrades.Items[i].SubItems.Add(t.d.ToString());
lstTrades.Items[i].SubItems.Add(Math.Round(e.pnl, 2).ToString());
lstTrades.Items[i].SubItems.Add(t.f.ToString());
lstTrades.Items[i].SubItems.Add(t.g.ToShortTimeString());
lstTrades.Items[i].SubItems.Add(t.h);
lstTrades.Items[i].SubItems.Add(t.i.ToString());
}
The sort code is a gently modified code from http://support.microsoft.com/kb/319401
using System.Collections;
using System.Windows.Forms;
using System;
namespace Aladmin2
{
/// <summary>
/// This class is an implementation of the 'IComparer' interface.
/// </summary>
public class ListViewColumnSorter : IComparer
{
/// <summary>
/// Specifies the column to be sorted
/// </summary>
private int ColumnToSort;
/// <summary>
/// Specifies the order in which to sort (i.e. 'Ascending').
/// </summary>
private SortOrder OrderOfSort;
/// <summary>
/// Case insensitive comparer object
/// </summary>
private CaseInsensitiveComparer ObjectCompare;
/// <summary>
/// Class constructor. Initializes various elements
/// </summary>
public ListViewColumnSorter()
{
// Initialize the column to '0'
ColumnToSort = 0;
// Initialize the sort order to 'none'
OrderOfSort = SortOrder.None;
// Initialize the CaseInsensitiveComparer object
ObjectCompare = new CaseInsensitiveComparer();
}
/// <summary>
/// This method is inherited from the IComparer interface. It compares the two objects passed using a case insensitive comparison.
/// </summary>
/// <param name="x">First object to be compared</param>
/// <param name="y">Second object to be compared</param>
/// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX, listviewY;
// Cast the objects to be compared to ListViewItem objects
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;
// Compare the two items
DateTime dateValue;
if (DateTime.TryParse(listviewX.SubItems[ColumnToSort].Text, out dateValue))
{
compareResult = DateTime.Compare(DateTime.Parse(listviewX.SubItems[ColumnToSort].Text), DateTime.Parse(listviewY.SubItems[ColumnToSort].Text));
}
else
{
compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
}
// Calculate correct return value based on object comparison
if (OrderOfSort == SortOrder.Ascending)
{
// Ascending sort is selected, return normal result of compare operation
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
{
// Descending sort is selected, return negative result of compare operation
return (-compareResult);
}
else
{
// Return '0' to indicate they are equal
return 0;
}
}
/// <summary>
/// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
/// </summary>
public int SortColumn
{
set
{
ColumnToSort = value;
}
get
{
return ColumnToSort;
}
}
/// <summary>
/// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
/// </summary>
public SortOrder Order
{
set
{
OrderOfSort = value;
}
get
{
return OrderOfSort;
}
}
}
}
EDIT:
I am only using 1 thread (that I know about/creating) in both updates and sorting
EDIT: In the article you mentioned, they have the line:
this.listView1.Sort();Try adding this to your
ProcessUpdatemethod, right at the end.This sorter doesn’t implement a continuous sort- you need to keep calling that as you add and remove data. Sorry we all jumped on you about threading.
EDIT #2: Also, try this for your
ProcessUpdatemethod:If you are not already, you need to marshal the `ProcessUpdate` method (and anything else that touches a bound list) to the UI thread. You can’t safely update these from a background thread, for the same reasons that you can’t touch UI controls from a background thread.
But no, the comparer is not running on it’s own thread.