Ejemplo n.º 1
0
        internal void Search(FileSearchParam param)
        {
            SearchStarted();

            _param = param;
            if (string.IsNullOrEmpty(param.RootDir))
            {
                var drives = DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.Fixed);
                foreach (var drive in drives)
                {
                    Search(drive.RootDirectory, param.SearchPattern);
                }
            }
            else
            {
                Search(new DirectoryInfo(param.RootDir), param.SearchPattern);
            }
        }
Ejemplo n.º 2
0
        public async void StartSearch(FileSearchParam param)
        {
            var workerTokenSource = new CancellationTokenSource();
            var token             = workerTokenSource.Token;

            var searcher = CreateSearcher(token, param.PlugName);

            searcher.OnSearchStarted += searcher_OnSearchStarted;
            searcher.OnFileFound     += searcher_OnFileFound;

            var worker = new SearchWorker
            {
                Id         = searcher.Id,
                FilesFound = 0,
                Parameter  = param,
                Status     = WorkerStatus.Pending.ToString()
            };

            SearchWorkers.Add(worker);
            WorkerTokenSources.Add(searcher.Id, workerTokenSource);
            try
            {
                await Task.Factory.StartNew(() => searcher.Search(param));
            }
            catch (OperationCanceledException)
            {
                // handle task cancelation
                Debug.WriteLine("Search canseled: {0}", searcher.Id);
                worker.Status = WorkerStatus.Stopped.ToString();
                return;
            }
            catch (Exception ex)
            {
                // other exception occured
                Debug.WriteLine(ex);
            }
            finally
            {
                WorkerTokenSources.Remove(searcher.Id);
            }
            worker.Status = WorkerStatus.Finished.ToString();
        }