ListView 성능 개선

This commit is contained in:
2018-10-11 17:27:45 +09:00
parent 1cd307b45a
commit 6ca9067be0
2 changed files with 54 additions and 53 deletions

View File

@@ -79,24 +79,17 @@ namespace NewsCrawler
int m_iCrawlInterval = 500;
bool m_bBuy = false;
List<string> m_URLs = new List<string>();
ConcurrentQueue<ListViewItem> m_NewItems = new ConcurrentQueue<ListViewItem>();
ConcurrentBag<string> m_URLs = new ConcurrentBag<string>();
System.Timers.Timer m_SystemTimer = new System.Timers.Timer();
ConcurrentQueue<PRICE_CHECK_DATA> m_PriceCheckList = new ConcurrentQueue<PRICE_CHECK_DATA>();
System.Timers.Timer m_PriceCheckTimer = new System.Timers.Timer();
delegate void InsertListView(string strTitle, string strName, string strCode, DateTime time, DateTime ResTime, string strURL, string strRef, double responseT, bool bInitial);
InsertListView m_InsertListViewDelegate = null;
Crawler m_Crawler = null;
ExcelHandler m_Excel = null;
object m_lvListLock = new object();
public NewsForm()
{
@@ -116,8 +109,6 @@ namespace NewsCrawler
//MessageBox.Show("Keywords : \n\n" + m_Condition.GetKeywordsCnt());
m_InsertListViewDelegate = new InsertListView(this.InsertItem);
m_Crawler.ReadKIND(true);
m_Crawler.ReadDart(true);
m_Crawler.ReadEtoday(true);
@@ -532,30 +523,21 @@ namespace NewsCrawler
if(strURL == "")
return false;
lock(m_URLs)
return m_URLs.Any(s => s == strURL);
return m_URLs.Any(s => s == strURL);
}
public void InsertItem(string strTitle, string strName, string strCode, DateTime time, DateTime ResTime, string strURL, string strRef, double dElapseT, bool bInitial)
{
try
{
if(this.InvokeRequired)
{
this.Invoke(m_InsertListViewDelegate, strTitle, strName, strCode, time, ResTime, strURL, strRef, dElapseT, bInitial);
}
else
{
if(IsDuplicatedURL(strURL) == true)
return;
if (IsDuplicatedURL(strURL) == true)
return;
int iID = lvList.Items.Count+1;
int iID = lvList.Items.Count + 1;
CodeList.CODE_VALUE Code = m_CodeList.SearchCode(strTitle);
if(bInitial == false && Code != null)
ProcessSearchAndBuy(new NEWS_ITEM(iID, strTitle, strCode, Code, time, ResTime, strRef, strURL, (float)dElapseT));
CodeList.CODE_VALUE Code = m_CodeList.SearchCode(strTitle);
if (bInitial == false && Code != null)
ProcessSearchAndBuy(new NEWS_ITEM(iID, strTitle, strCode, Code, time, ResTime, strRef, strURL, (float)dElapseT));
var AddedItem = lvList.Items.Add(new ListViewItem(new string[] {
var newItem = new ListViewItem(new string[] {
iID.ToString(),
time.ToString("HH:mm:ss"),
ResTime.ToString("HH:mm:ss:fff"),
@@ -568,31 +550,42 @@ namespace NewsCrawler
"",
"",
"",
strURL }));
strURL });
newItem.UseItemStyleForSubItems = false;
if (Code == null && strCode != "")
newItem.SubItems[chCodeName.Index].BackColor = Color.Gray;
AddedItem.UseItemStyleForSubItems = false;
if(Code == null && strCode != "")
AddedItem.SubItems[chCodeName.Index].BackColor = Color.Gray;
m_NewItems.Enqueue(newItem);
m_URLs.Add(strURL);
if(lvList.Items.Count < 2)
Action insert = delegate
{
if (m_NewItems.IsEmpty == false)
{
List<ListViewItem> itemList = new List<ListViewItem>();
ListViewItem item;
while (m_NewItems.TryDequeue(out item))
itemList.Add(item);
lvList.BeginUpdate();
lvList.Items.AddRange(itemList.ToArray());
if (lvList.Items.Count < 2)
{
foreach(ColumnHeader col in lvList.Columns)
foreach (ColumnHeader col in lvList.Columns)
col.Width = -2;
}
lock(m_URLs)
m_URLs.Add(strURL);
if(bInitial == false)
if (bInitial == false)
lvList.Sort();
}
}
catch(Exception ex)
{
Util.Log(Util.LOG_TYPE.ERROR, ex.Message + Environment.NewLine + ex.StackTrace);
}
}
lvList.EndUpdate();
}
};
if (lvList.InvokeRequired)
lvList.Invoke(insert);
else
insert();
}
private void SystemTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
@@ -639,6 +632,9 @@ namespace NewsCrawler
private void UpdateStartPrice(int iID, int iPrice)
{
if (iID < 0)
return;
if(lvList.InvokeRequired)
{
lvList.BeginInvoke((MethodInvoker)delegate ()
@@ -774,9 +770,11 @@ namespace NewsCrawler
{
SortOrder Order = (lvList.Sorting == SortOrder.Descending || lvList.Sorting == SortOrder.None) ? SortOrder.Ascending : SortOrder.Descending;
lvList.BeginUpdate();
lvList.ListViewItemSorter = new ListViewItemComparer(e.Column, Order);
lvList.Sorting = Order;
lvList.Sort();
lvList.EndUpdate();
}
private void lvList_DoubleClick(object sender, EventArgs e)
@@ -1033,7 +1031,10 @@ namespace NewsCrawler
if(double.TryParse(item1.SubItems[m_iColumn].Text, out num1) &&
double.TryParse(item2.SubItems[m_iColumn].Text, out num2))
{
return (num1>num2)?1:-1;
if (m_Order == SortOrder.Ascending)
return (num1 > num2) ? 1 : -1;
else
return (num1 > num2) ? -1 : 1;
}
else
{

View File

@@ -14,16 +14,16 @@ namespace NewsCrawler
[STAThread]
static void Main()
{
//try
//{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new NewsForm());
//}
//catch(Exception ex)
//{
// Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
//}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
}
}
}
}