/// <summary> /// Загружает данные с рабочего каталога. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void LoadDataFromFiles_OnClick(object sender, RoutedEventArgs e) { object lockObj = new object(); BindingOperations.EnableCollectionSynchronization(BankProv.BankObj.LegalPersonsDepartment.Clients, lockObj); BindingOperations.EnableCollectionSynchronization(BankProv.BankObj.PhysicalPersonsDepartment.Clients, lockObj); Action <object> action = o => { Data data = o as Data; string json = File.ReadAllText(data.File); var bankProvider = JsonConvert.DeserializeObject <BankProvider>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, NullValueHandling = NullValueHandling.Ignore }); lock (lockObj) { BankProv += bankProvider; } }; string[] files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.clients"); // Готовим потоки. int threadsCount = Math.Min(files.Length, Environment.ProcessorCount - 1); List <Thread> threads = new List <Thread>(); for (int i = 0; i < threadsCount; i++) { var parameterizedThreadStart = new ParameterizedThreadStart(action); threads.Add(new Thread(parameterizedThreadStart)); } ProgressManager progressManager = new ProgressManager(); progressManager.BeginWaiting(); progressManager.SetProgressMaxValue(threads.Count); for (int i = 0; i < threads.Count; i++) { threads[i].Start(new Data(files[i])); } for (int i = 0; i < threads.Count; i++) { threads[i].Join(); progressManager.ChangeProgress(i + 1); } Thread.Sleep(100); progressManager.EndWaiting(); }
private void button1_Click(object sender, RoutedEventArgs e) { ProgressManager pm = new ProgressManager(); pm.BeginWaiting(); pm.SetProgressMaxValue(10); for (int i = 0; i < 10; i++) { pm.ChangeStatus("Loading " + i.ToString() + " from 10"); pm.ChangeProgress(i); Thread.Sleep(1000); } pm.EndWaiting(); }
/// <summary> /// Генерирует большое количество данных. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void GenerateDataButton_OnClick(object sender, RoutedEventArgs e) { Random random = new Random(); ObservableCollection <Client> newClients = new ObservableCollection <Client>(); int n = 10000; ProgressManager progressManager = new ProgressManager(); progressManager.BeginWaiting(); progressManager.ChangeStatus("В отдельном потоке выполняем генерацию записей..."); progressManager.SetProgressMaxValue(n); for (int i = 0; i < n; i++) { ClientBase client = Convert.ToBoolean(random.Next(1, 10) % 2 == 0) ? (ClientBase) new Client(Guid.NewGuid().ToString()) : new VipClient(Guid.NewGuid().ToString()); newClients.Add(new Client(Guid.NewGuid().ToString())); BankProv.BankObj.AddNewClient(client, random.Next(1, 10) % 2 != 0); progressManager.ChangeProgress(i); } progressManager.EndWaiting(); }