簡體   English   中英

如何在單獨的線程中初始化類

[英]How can I initialize class in a separate thread

我有一個類storedetails,它從數據庫中獲取不同的值。 從數據庫中獲取這些值並進行計算需要很長時間(由於對一個商店的76個不同查詢的數據庫設計不佳)。

我生成報告的實現如下。

string week = "32";
string year = "2013";
string[] storecode = getallstoreforweekandyear(week, year); // get all store code that were active for the week and year
ArrayList ar = new ArrayList();
foreach (string item in storecode)
{
    storedetails sd = new storedetails(item, year, week);// this initializion I want to move to another thread because it is taking time.
    ar.Add(sd);
}

ar.TrimToSize();
DataTable dt = getdtfromarraylist(ar);// convert the arraylist of class to datatable
     Gridview1.Datasourc=dt; 

我的類的實現是在2000行以上的代碼中

class storedetails
{
    public storedetails(string store,string year, string week)
{ 

//time taking implementation
}
}

類的初始化是否有可能在單獨的線程中發生,所以我可以提高速度嗎?

您是否已簽出.NET 4.0中的任務並行庫-它們大大簡化了線程處理。

http://msdn.microsoft.com/zh-CN/library/dd460717(v=vs.110).aspx

object locker = new object();
Parallel.ForEach (storecode => item
     {
          storedetails sd = new storedetails(item, year, week);
          lock(locker)
          {
              ar.Add(sd);
          }
     });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM