void AssignTasks() { #region SORT label1.Text = "(1/5) Sorting, removing duplicates and place mean value for duplicates..."; //Creating Output directory DirectoryInfo Dir = new DirectoryInfo(SourcePath.Text); Directory.CreateDirectory(SourcePath.Text + @"\Sorted"); int TotalFiles = Dir.GetFiles().Length; if (TotalFiles % 5 != 0) { error.Visible = true; } //Splitting all files into 4 part to assign to Workers by Array 'Take' and 'Skip' FileInfo[] FI = Dir.GetFiles(); FileInfo[] temp1 = FI.Take(FI.Length / 2).ToArray(); FileInfo[] temp2 = FI.Skip(FI.Length / 2).ToArray(); firstArray = temp1.Take(temp1.Length / 2).ToArray(); secondArray = temp1.Skip(temp1.Length / 2).ToArray(); thirdArray = temp2.Take(temp2.Length / 2).ToArray(); forthArray = temp2.Skip(temp2.Length / 2).ToArray(); //Running Workers SortWorker1.RunWorkerAsync(firstArray); SortWorker2.RunWorkerAsync(secondArray); SortWorker3.RunWorkerAsync(thirdArray); SortWorker4.RunWorkerAsync(forthArray); #endregion }
private void SortWorker1_DoWork(object sender, DoWorkEventArgs e) { int Counter = 0; int TotalFiles = (e.Argument as FileInfo[]).Length; foreach (FileInfo fi in e.Argument as FileInfo[]) { Counter++; if (fi.Extension == ".txt") { //Some Heavy tasks } SortWorker1.ReportProgress((Counter * 100) / TotalFiles); } }