Exemple #1
0
 private async void FindRun(Object obj)
 {
     TimerForWPF.Stop();
     TimerForWPF.Start();
     Model.RootFileOrFolder = new FileOrFolder
     {
         Name              = RootFolder,
         IsFile            = false,
         Path              = RootFolder,
         ChldFileOrFolders = new System.Collections.ObjectModel.ObservableCollection <FileOrFolder>()
     };
     Model.FilterForSearch = new FilterForSearch {
         Name = TemplateNameFile ?? "*.*", Content = ContentFile ?? ""
     };
     FileOrFolder.Clear();
     if (await Model.SearchRun())
     {
         TimerForWPF.Pause();
         if (Model.RootFileOrFolder != null)
         {
             FileOrFolder.Add(Model.RootFileOrFolder);
         }
         OnPropertyChanged("FileOrFolder");
     }
 }
Exemple #2
0
        /// <summary>
        /// Заполнение коллекции FileOrFolder дочерних файлов и папок
        /// </summary>
        /// <param name="fileOrFolder"></param>
        /// <param name="cancellationToken"></param>
        /// <returns>ObservableCollection<FileOrFolder>, если null то выполнение быол прервано</returns>
        ObservableCollection <FileOrFolder> FillChildren(FileOrFolder fileOrFolder, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }
            PauseEvent.WaitOne();
            ObservableCollection <FileOrFolder> childFileOrFolders = new ObservableCollection <FileOrFolder>();
            DirectoryInfo currentDirectoryInfo = new DirectoryInfo(fileOrFolder.Path);

            SelectFiles(currentDirectoryInfo.GetFiles(FilterForSearch.Name), out childFileOrFolders, cancellationToken);
            List <FileOrFolder> listDirectoryFileOrFolder = currentDirectoryInfo.GetDirectories().Select(c =>
                                                                                                         new FileOrFolder
            {
                Name              = c.Name,
                IsFile            = false,
                Path              = c.FullName,
                ChldFileOrFolders = new ObservableCollection <FileOrFolder>()
            }
                                                                                                         ).ToList();
            List <Task> tasks = new List <Task>();

            listDirectoryFileOrFolder.ForEach(DirectoryFileOrFolder =>
            {
                childFileOrFolders.Add(DirectoryFileOrFolder);
                tasks.Add(SearchFileOrFolder(DirectoryFileOrFolder));
            });
            Task.WaitAll(tasks.ToArray());
            return(childFileOrFolders);
        }
Exemple #3
0
        /// <summary>
        /// стартовый метод поиска для внутреннего вызова,
        ///  поиск в поиске
        /// </summary>
        /// <param name="fileOrFolder"></param>
        /// <returns>bool</returns>
        private async Task <bool> SearchFileOrFolder(FileOrFolder fileOrFolder)
        {
            CancellationToken cancellationToken = CancellationTokenSource.Token;

            fileOrFolder = await Search(fileOrFolder, cancellationToken);

            return(true);
        }
Exemple #4
0
        /// <summary>
        /// Стартовый метод поиска
        /// </summary>
        /// <param name="fileOrFolder"></param>
        /// <param name="cancellationToken"></param>
        /// <returns>Task<FileOrFolder>, если null то выполнение было прервано</returns>
        public async Task <FileOrFolder> Search(FileOrFolder fileOrFolder, CancellationToken cancellationToken)
        {
            try
            {
                fileOrFolder.ChldFileOrFolders = await Task.Run(() => FillChildren(fileOrFolder, cancellationToken));

                if (cancellationToken.IsCancellationRequested)
                {
                    Console.WriteLine($"Поток {Thread.CurrentThread.ManagedThreadId} отменён");
                    return(null);
                }
                return(fileOrFolder);
            }
            catch (Exception)
            {
                Console.WriteLine("прервано");
                return(null);
            }
        }
Exemple #5
0
        /// <summary>
        /// Заполнение коллекции FileOrFolder
        /// </summary>
        /// <param name="fileInfos"></param>
        /// <param name="fileOrFolders"></param>
        private void SelectFiles(FileInfo[] fileInfos, out ObservableCollection <FileOrFolder> fileOrFolders,
                                 CancellationToken cancellationToken)
        {
            Console.WriteLine(Thread.CurrentThread.ManagedThreadId);

            if (String.IsNullOrEmpty(FilterForSearch.Content))
            {
                fileOrFolders = FileOrFolder.FillResultCollection(fileInfos);
                return;
            }
            var selectedFileInfo = fileInfos.Where(c =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(false);
                }
                Thread.Sleep(new Random().Next(2000, 5000));//TODO для эмуляции загруженности
                Mutex.WaitOne();
                FileNameInProcess = c.Name;
                Mutex.ReleaseMutex();
                Console.WriteLine(FileNameInProcess);
                using (StreamReader strmRead = c.OpenText())
                {
                    bool thisFileComtent = false;
                    try
                    {
                        string textFile = strmRead.ReadToEnd();
                        thisFileComtent = FindText(textFile, FilterForSearch.Content);
                    }
                    catch (Exception)
                    { }
                    return(thisFileComtent);
                }
            }).ToList();

            fileOrFolders = FileOrFolder.FillResultCollection(selectedFileInfo);
        }