public List <string> FindData(IEnumerable <IEntity> fileNames, CancellationTokenSource cts = null, IProgress <int> progressChanger = null) { List <string> res = new List <string>(); Object lockThis = new Object(); try { Parallel.ForEach(fileNames, file => { IVisitor findPersData = new FindPersDataVisit(); file.Accept(findPersData); lock (lockThis) { foreach (var el in file.PersData) { res.Add(el); } } if (cts != null) { cts.Token.ThrowIfCancellationRequested(); progressChanger.Report(50); } }); } catch (OperationCanceledException) { return(new List <string>()); } return(res); }
private Task <List <string> > findPersonDataAsync(IEntity file, CancellationToken cancellationToken) { return(Task.Run(() => { IVisitor findPersData = new FindPersDataVisit(); if (cancellationToken != null) { cancellationToken.ThrowIfCancellationRequested(); } file.Accept(findPersData); return file.PersData; })); }
private void ThreadFunc(CancellationTokenSource cts) { foreach (var file in nameOfFiles) { IVisitor findPersData = new FindPersDataVisit(); file.Accept(findPersData); foreach (string el in file.PersData) { res.Add(el); } } }
public List <string> FindData(IEnumerable <IEntity> fileNames, CancellationTokenSource cts = null, IProgress <int> progressChanger = null) { List <string> res = new List <string>(); try { List <Task <List <string> > > tasks = new List <Task <List <string> > >(); int count = 0, totalCount = fileNames.Count(); foreach (var file in fileNames) { Task <List <string> > findTask = Task.Run(() => { IVisitor findPersData = new FindPersDataVisit(); file.Accept(findPersData); return(file.PersData); }); tasks.Add(findTask); } foreach (var t in tasks) { List <string> result = t.Result; cts.Token.ThrowIfCancellationRequested(); progressChanger.Report(count++ *100 / totalCount); foreach (string str in result) { res.Add(str); } } } catch (OperationCanceledException) { return(new List <string>()); } return(res); }