code_junkie

Simplest way to process a list of items in a multi-threaded manner

Nov 14th, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. using (IDataReader dr = q.ExecuteReader())
  2. {
  3. ThreadPool.SetMaxThreads(10, 10);
  4. int workerThreads = 0;
  5. int completionPortThreads = 0;
  6. while (dr.Read())
  7. {
  8. do
  9. {
  10. ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
  11.  
  12. if (workerThreads == 0)
  13. {
  14. Thread.Sleep(100);
  15. }
  16. } while (workerThreads == 0);
  17.  
  18. Database.Log l = new Database.Log();
  19. l.Load(dr);
  20.  
  21. ThreadPool.QueueUserWorkItem(delegate(object threadContext)
  22. {
  23. Database.Log log = threadContext as Database.Log;
  24. Scraper scraper = new Scraper();
  25. dc.Product p = scraper.GetProduct(log, log.Url, true);
  26. ManualResetEvent done = new ManualResetEvent(false);
  27. done.Set();
  28. }, l);
  29. }
  30. }
Add Comment
Please, Sign In to add comment