Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. void ConcurrentDicTest()
  2. {
  3. ConcurrentDictionary<string, string> concurrDic = new ConcurrentDictionary<string, string>();
  4.  
  5. Task tsk1 = Task.Factory.StartNew(() =>
  6. {
  7. for (int i = 0; i < 100; i++)
  8. {
  9. concurrDic.TryAdd(i.ToString(), (i + i).ToString());
  10. Thread.Sleep(100);
  11. }
  12. });
  13.  
  14. Task tsk2 = Task.Factory.StartNew(() =>
  15. {
  16. Thread.Sleep(300);
  17. foreach (var item in concurrDic)
  18. {
  19. Console.WriteLine($"{item.Key} - {item.Value}");
  20. Thread.Sleep(150);
  21. }
  22. });
  23.  
  24. try
  25. {
  26. Task.WaitAll(tsk1, tsk2);
  27. }
  28. catch (AggregateException agExc)
  29. {
  30. Console.WriteLine(agExc.Flatten().Message);
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement