private static ConcurrentBag <Oy> GenerateRandomDataUsingTasks() { var arr = new ConcurrentBag <Oy>(); // Thread Safe List var tasks = new List <Task>(); for (int i = 0; i < CIller.Length; i++) { var ilAdi = CIller[i]; var task = Task.Run(() => { for (int j = 0; j < CIlceler.Length; j++) { for (int k = 0; k < CMahalleler.Length; k++) { for (int l = 0; l < COkullar.Length; l++) { for (int m = 0; m < CSandiklar.Length; m++) { for (int n = 0; n < _count; n++) { var oy = new Oy() { Id = Guid.NewGuid(), Il = ilAdi, Ilce = CIlceler[j], Mahalle = CMahalleler[k], Okul = COkullar[l], SandikNo = CSandiklar[m], Parti = GenerateRandomPartyName() }; Thread.Sleep(_delayMs); arr.Add(oy); } } } } } }); tasks.Add(task); } Task.WaitAll(tasks.ToArray()); return(arr); }
private static ConcurrentBag <Oy> GenerateRandomDataUsingParallelLoop() { var arr = new ConcurrentBag <Oy>(); // Thread Safe List for (int i = 0; i < CIller.Length; i++) { Parallel.For(0, CIller.Length, i => { Parallel.For(0, CIlceler.Length, j => { Parallel.For(0, CMahalleler.Length, k => { Parallel.For(0, COkullar.Length, l => { Parallel.For(0, CSandiklar.Length, m => { Parallel.For(0, _count, n => { var oy = new Oy() { Id = Guid.NewGuid(), Il = CIller[i], Ilce = CIlceler[j], Mahalle = CMahalleler[k], Okul = COkullar[l], SandikNo = CSandiklar[m], Parti = GenerateRandomPartyName() }; Thread.Sleep(_delayMs); arr.Add(oy); }); }); }); }); }); }); } return(arr); }
private static List <Oy> GenerateRandomData() { var arr = new List <Oy>(); for (int i = 0; i < CIller.Length; i++) { for (int j = 0; j < CIlceler.Length; j++) { for (int k = 0; k < CMahalleler.Length; k++) { for (int l = 0; l < COkullar.Length; l++) { for (int m = 0; m < CSandiklar.Length; m++) { for (int n = 0; n < _count; n++) { var oy = new Oy() { Id = Guid.NewGuid(), Il = CIller[i], Ilce = CIlceler[j], Mahalle = CMahalleler[k], Okul = COkullar[l], SandikNo = CSandiklar[m], Parti = GenerateRandomPartyName() }; Thread.Sleep(_delayMs); arr.Add(oy); } } } } } } return(arr); }