code_junkie

Do you know a Bulked/Batched Flows Library for C#

Nov 10th, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. input = Read();
  2. parsed = Parse(input);
  3. if (parsed.Count > 10)
  4. {
  5. status = Persist(parsed);
  6. ReportSuccess(status);
  7. return;
  8. }
  9. ReportFailure();
  10.  
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using System.ComponentModel;
  15. using System.Threading;
  16.  
  17. class PersistenceManager
  18. {
  19. public void Persist(ICollection persistable)
  20. {
  21. // initialize a list of background workers
  22. var backgroundWorkers = new List<BackgroundWorker>();
  23.  
  24. // launch each persistable item in a background worker on a separate thread
  25. foreach (var persistableItem in persistable)
  26. {
  27. var worker = new BackgroundWorker();
  28. worker.DoWork += new DoWorkEventHandler(worker_DoWork);
  29. backgroundWorkers.Add(worker);
  30. worker.RunWorkerAsync(persistableItem);
  31. }
  32.  
  33. // wait for all the workers to finish
  34. while (true)
  35. {
  36. // sleep a little bit to give the workers a chance to finish
  37. Thread.Sleep(100);
  38.  
  39. // continue looping until all workers are done processing
  40. if (backgroundWorkers.Exists(w => w.IsBusy)) continue;
  41.  
  42. break;
  43. }
  44.  
  45. // dispose all the workers
  46. foreach (var w in backgroundWorkers) w.Dispose();
  47. }
  48.  
  49. void worker_DoWork(object sender, DoWorkEventArgs e)
  50. {
  51. var persistableItem = e.Argument;
  52. // TODO: add logic here to save the persistableItem to the database
  53. }
  54. }
Add Comment
Please, Sign In to add comment