public MainWindow() { InitializeComponent(); _pool = CustomThreadPool.GetInstance(MinThreadCount, MaxThreadCount); _accumulator = new ConcurrentAccumulator(1000, 6000, OnFlush); _accumulator.OnObjectCountChange += UpdateObjectCount; LThreads.Content = $"Thread count: {_handles.Count}"; }
private void CopyFolder(string sourcePath, string destinationPath) { var dir = new DirectoryInfo(sourcePath); foreach (var directory in dir.GetDirectories()) { var newDir = new DirectoryInfo(Path.Combine(destinationPath, directory.Name)); Dispatcher.Invoke(() => LbLog.Items.Add("Create " + newDir.FullName)); newDir.Create(); CopyFolder(directory.FullName, newDir.FullName); } foreach (var file in dir.GetFiles()) { _countTotal++; Dispatcher.Invoke(() => PbProgress.Maximum = _countTotal); try { var pool = CustomThreadPool.GetInstance(); var destination = Path.Combine(destinationPath, file.Name); pool.QueueUserTask( () => { file.CopyTo(destination, true); return(true); }, ts => { lock (this) { if (ts.Success) { _countCopied++; } var message = file.FullName + " -> " + destination; Dispatcher.Invoke(() => { LbLog.Items.Add(ts.Pid.ToString().PadRight(5) + (ts.Success ? "Success: " : "Fail: ").PadRight("Success: ".Length) + message + (ts.Success ? "" : " : " + ts.InnerException.Message)); LbLog.Items.MoveCurrentToLast(); LbLog.ScrollIntoView(LbLog.Items.CurrentItem); PbProgress.Value = _countCopied; LProgress.Content = $"{_countCopied}/{_countTotal}"; }); } }); } catch (Exception ex) { LbLog.Items.Add(ex.Message); } } }
private void SaveThreadsCount_Click(object sender, RoutedEventArgs e) { if (!IudThreadCount.Value.HasValue) { return; } BSave.IsEnabled = false; IudThreadCount.IsReadOnly = true; _pool = CustomThreadPool.GetInstance(IudThreadCount.Value.Value, IudThreadCount.Value.Value); }
private void Window_Closing(object sender, CancelEventArgs e) { CustomThreadPool.GetInstance().Dispose(); }