Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void ConcurrentDicTest()
- {
- ConcurrentDictionary<string, string> concurrDic = new ConcurrentDictionary<string, string>();
- Task tsk1 = Task.Factory.StartNew(() =>
- {
- for (int i = 0; i < 100; i++)
- {
- concurrDic.TryAdd(i.ToString(), (i + i).ToString());
- Thread.Sleep(100);
- }
- });
- Task tsk2 = Task.Factory.StartNew(() =>
- {
- Thread.Sleep(300);
- foreach (var item in concurrDic)
- {
- Console.WriteLine($"{item.Key} - {item.Value}");
- Thread.Sleep(150);
- }
- });
- try
- {
- Task.WaitAll(tsk1, tsk2);
- }
- catch (AggregateException agExc)
- {
- Console.WriteLine(agExc.Flatten().Message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement