/// <summary> /// Constructor. /// </summary> public void ChangeTextIntoTextBox(TextBox[] txtBoxs, User u) { txtBoxs[0].Text = u.First_Name; txtBoxs[1].Text = u.Last_Name; txtBoxs[2].Text = u.Age.ToString(); txtBoxs[3].Text = u.Bio; txtBoxs[4].Text = u.Country; txtBoxs[5].Text = u.City; }
/// <summary> /// Methods of adding a record in the database (Storage3). /// Called from the methods TransactionScopeWithStorage. /// </summary> /// <param name="context">The context of the interaction with the database.</param> /// <param name="entity">The object to add the type of User.</param> /// <param name="count">Counter.</param> /// <param name="commitCount">Meaning - "every time commitCount doing SaveChanges()".</param> /// <param name="recreateContext">It must be true.</param> /// <returns>The context of the interaction with the database.</returns> private static Storage3Context AddToStorage3(Storage3Context context, User entity, int count, int commitCount, bool recreateContext) { context.Set<User>().Add(entity); if (count%commitCount != 0) return context; context.SaveChanges(); if (!recreateContext) return context; context.Dispose(); context = new Storage3Context(); context.Configuration.AutoDetectChangesEnabled = false; return context; }
/// <summary> /// Event: Complete database. /// </summary> /// <param name="sender">Sender</param> /// <param name="e">Event Args</param> override protected void btnFill_Click(object sender, RoutedEventArgs e) { #region The code required for diagnosis - measures the time var spectator = new Stopwatch(); spectator.Start(); #endregion var fd = new FillingData(); int i, bl; var name = File.ReadAllLines("name.txt"); var surname = File.ReadAllLines("surname.txt"); var book1 = TruncateLongString(string.Join(" ", File.ReadAllLines("book-1.txt")), 1000).ToArray(); var book2 = TruncateLongString(string.Join(" ", File.ReadAllLines("book-2.txt")), 1000).ToArray(); var rnd = new Random(); var usersForStorage1 = new List<User>(); var usersForStorage2 = new List<User>(); var usersForStorage3 = new List<User>(); var usersForStorage4 = new List<User>(); var usersForStorage5 = new List<User>(); for (i = 0; i < book1.Length; i++) { var user = new User { Id = i + 1, First_Name = name[rnd.Next(name.Length)], Last_Name = surname[rnd.Next(surname.Length)], Age = rnd.Next(14, 80), Country = "Russia", City = (rnd.Next(0, 2) == 0) ? "St. Petersburg" : "Moscow", Bio = book1[i] }; usersForStorage1.Add(user); } for (bl = 0; i < 100000; i++, bl++) { if (!(bl < book2.Length)) bl = 0; var user = new User { Id = i + 1, First_Name = name[rnd.Next(name.Length)], Last_Name = surname[rnd.Next(surname.Length)], Age = rnd.Next(14, 80), Country = "Russia", City = (rnd.Next(0, 2) == 0) ? "St. Petersburg" : "Moscow", Bio = book2[bl] }; usersForStorage1.Add(user); } for (i = 100000; i < 200000; i++, bl++) { if (!(bl < book2.Length)) bl = 0; var user = new User { Id = i + 1, First_Name = name[rnd.Next(name.Length)], Last_Name = surname[rnd.Next(surname.Length)], Age = rnd.Next(14, 80), Country = "Russia", City = (rnd.Next(0, 2) == 0) ? "St. Petersburg" : "Moscow", Bio = book2[bl] }; usersForStorage2.Add(user); } for (i = 200000; i < 300000; i++, bl++) { if (!(bl < book2.Length)) bl = 0; var user = new User { Id = i + 1, First_Name = name[rnd.Next(name.Length)], Last_Name = surname[rnd.Next(surname.Length)], Age = rnd.Next(14, 80), Country = "Russia", City = (rnd.Next(0, 2) == 0) ? "St. Petersburg" : "Moscow", Bio = book2[bl] }; usersForStorage3.Add(user); } for (i = 300000; i < 400000; i++, bl++) { if (!(bl < book2.Length)) bl = 0; var user = new User { Id = i + 1, First_Name = name[rnd.Next(name.Length)], Last_Name = surname[rnd.Next(surname.Length)], Age = rnd.Next(14, 80), Country = "Russia", City = (rnd.Next(0, 2) == 0) ? "St. Petersburg" : "Moscow", Bio = book2[bl] }; usersForStorage4.Add(user); } for (i = 400000; i < 500000; i++, bl++) { if (!(bl < book2.Length)) bl = 0; var user = new User { Id = i + 1, First_Name = name[rnd.Next(name.Length)], Last_Name = surname[rnd.Next(surname.Length)], Age = rnd.Next(14, 80), Country = "Russia", City = (rnd.Next(0, 2) == 0) ? "St. Petersburg" : "Moscow", Bio = book2[bl] }; usersForStorage5.Add(user); } var transactionScopeWithStorage1 = new Task(() => fd.TransactionScopeWithStorage1(usersForStorage1)); var transactionScopeWithStorage2 = new Task(() => fd.TransactionScopeWithStorage2(usersForStorage2)); var transactionScopeWithStorage3 = new Task(() => fd.TransactionScopeWithStorage3(usersForStorage3)); var transactionScopeWithStorage4 = new Task(() => fd.TransactionScopeWithStorage4(usersForStorage4)); var transactionScopeWithStorage5 = new Task(() => fd.TransactionScopeWithStorage5(usersForStorage5)); transactionScopeWithStorage1.Start(); transactionScopeWithStorage2.Start(); transactionScopeWithStorage3.Start(); transactionScopeWithStorage4.Start(); transactionScopeWithStorage5.Start(); Task.WaitAll(transactionScopeWithStorage1, transactionScopeWithStorage2, transactionScopeWithStorage3, transactionScopeWithStorage4, transactionScopeWithStorage5); #region The code required for diagnosis - measures the time spectator.Stop(); var ts = spectator.Elapsed; lblTime.Content = $"Lead time: {ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds/10:00}"; #endregion }