我正在使用實現IComparer
接口的通用ListView分揀機。IComparer接口是否在C#中的單獨線程上工作?
這工作在一個單獨的線程從主線程?
我有一些時髦的結果。它對靜態ListView進行排序很好,但是,一旦它被填充流數據(它訂閱了一些不斷向其添加項目的事件),比較失敗並且變得怪異。
如果它在一個單獨的線程上 - 關於如何修改它的任何想法,以便它不會影響填充結果(反之亦然)?
或者如果它是在相同的線程上,爲什麼會發生這種情況的任何想法?
下面是更新的ListView(lstTrades
)
EDIT用於更新方法的代碼:我粘貼錯碼本來!!
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());
}
排序的代碼是從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;
}
}
}
輕輕修改後的代碼}
編輯:
我只使用1個線程(我知道/創建)的這兩個更新和排序
我不認爲這是一個線程問題 - 這只是一個紅鯡魚(在這種情況下)。添加新結果後,您不需要重新排序。 –
請注意,我最初發布了錯誤的更新代碼,其中一個是生成可正常工作的靜態列表的catchup代碼。尊敬的新代碼 – Sam
@Chris Shain--並不打算讓它成爲紅鯡魚。只是抓我的頭,看看爲什麼發生這種情況 – Sam