【发布时间】:2012-02-05 18:27:19
【问题描述】:
有了这个代码,很长一段时间以来,他都在不断地修补,他工作稳定而迅速。但似乎无法加快速度。很慢...
for (int jrCnt = rCnt; jrCnt <= arrayTable.GetUpperBound(0); jrCnt++)
{
var prcI = new Price();
/* here is the code search and add data to prcI */
if ((!string.IsNullOrEmpty(prcI.name)) && (prcI.prc != 0))
{ // function add
/* adding more information to prcI */
ThreadPool.QueueUserWorkItem(delegate
{
if (!Accessor.AddProductUpdateProduct(prcI)) _updateCounter++;
_countadd++;
}); // I put the longest function in the streams
}
}
这里我们调用函数。即使使用线程池,它也会运行很长时间。
public static bool AddProductUpdateProduct(Price price)
{
using (var db = new PriceDataContext())
{
var matchedprod =
db.Price.Where(x => x.name == price.name && x.company == price.company && x.date != price.date);
if (matchedprod.Select(x=>x).Count() > 1)
{
db.Price.DeleteOnSubmit(matchedprod.First());
db.SubmitChanges();
}
var matchedproduct = matchedprod.SingleOrDefault();
if (matchedproduct != null)
{
matchedproduct.date = price.date;
matchedproduct.prc = price.prc;
db.SubmitChanges();
return false;
}
}
/*here the code to add the product to the database.*/
return true;
}
请告诉我如何加快线程池的工作速度?
【问题讨论】:
-
你真正想用你的代码做什么?首先,您查询某些项目的数据上下文。 如果返回的项目计数>1,则您再次查询数据上下文并获取第一个项目。然后在那之后,您在原始查询上运行
SingleOrDefault()并做一些事情?这根本没有意义......对于初学者来说,为什么要运行两次相同的查询?其次,如果您输入第一个 if 语句,您将永远输入第二个...如果matchedprod.Count()>2 会怎样?你只删除一项?这么多问题...
标签: c# optimization threadpool