public Task CalculateFiles(string filePath, string CalculationStopped = null) { return(Task.Run(() => { try { if (isWork) { return; } isWork = true; ConcurrentBag <string> DirPaths = FolderManager.GetAllFilesFromSubfolder(filePath); var res = Calculate(DirPaths).Result; Report = new XmlDataWriter(); Report.ProcessNotifier += Main_ProcessEventNotifier; Report.GroupAndWriteFiles(res, DataFileName ?? "").GetAwaiter().GetResult(); } catch (Exception e) { if (e.InnerException is StopException) { FileProgressNotifier?.Invoke(string.IsNullOrEmpty(CalculationStopped)?"Calculation stopped":CalculationStopped, 0, 100); } if (e.InnerException is ReportException) { throw e; } throw e; } finally { isWork = false; GC.Collect(); } })); }