Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 1.65 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Optimization program using ThreadPool. C#
  2. for (int jrCnt = rCnt; jrCnt <= arrayTable.GetUpperBound(0); jrCnt++)
  3. {
  4.     var prcI = new Price();
  5.  
  6.     /* here is the code search and add data to prcI */
  7.  
  8.     if ((!string.IsNullOrEmpty(prcI.name)) && (prcI.prc != 0))
  9.     { // function add
  10.  
  11.         /* adding more information to prcI */
  12.  
  13.         ThreadPool.QueueUserWorkItem(delegate
  14.         {
  15.             if (!Accessor.AddProductUpdateProduct(prcI)) _updateCounter++;
  16.             _countadd++;
  17.         }); // I put the longest function in the streams
  18.     }
  19. }
  20.        
  21. public static bool AddProductUpdateProduct(Price price)
  22. {
  23.    using (var db = new PriceDataContext())
  24.         {
  25.             var matchedprod =
  26.                 db.Price.Where(x => x.name == price.name && x.company == price.company && x.date != price.date);
  27.  
  28.             if (matchedprod.Select(x=>x).Count() > 1)
  29.             {
  30.                 db.Price.DeleteOnSubmit(matchedprod.First());
  31.                 db.SubmitChanges();
  32.             }
  33.  
  34.             var matchedproduct = matchedprod.SingleOrDefault();
  35.  
  36.             if (matchedproduct != null)
  37.             {
  38.                 matchedproduct.date = price.date;
  39.                 matchedproduct.prc = price.prc;
  40.  
  41.                 db.SubmitChanges();
  42.                 return false;
  43.             }
  44.         }
  45.  
  46.  
  47. /*here the code to add the product to the database.*/
  48. return true;
  49. }
  50.        
  51. if (matchedprod.SingleOrDefault() != null)
  52. {
  53.     matchedprod.SingleOrDefault().date = price.date;
  54.     matchedprod.SingleOrDefault().prc = price.prc;
  55.  
  56.     db.SubmitChanges();
  57.     return false;
  58. }
  59.        
  60. var matchedprod = db.Price.Where(x => x.name == price.name && x.company == price.company && x.date != price.date)