Пример #1
0
 void searcher_OnSearchStarted(object sender, EventArgs e)
 {
     lock (_syncRoot)
     {
         var worker = SearchWorkers.SingleOrDefault(w => w.Id == ((FileSearcher)sender).Id);
         if (worker != null)
         {
             worker.Status = WorkerStatus.Running.ToString();
         }
     }
 }
Пример #2
0
 private void searcher_OnFileFound(object sender, FileFoundArgs e)
 {
     lock (_syncRoot)
     {
         var worker = SearchWorkers.SingleOrDefault(w => w.Id == e.SearcherId);
         if (worker != null)
         {
             worker.FilesFound++;
         }
         FileFound(e.SearcherId, e.FileName);
     }
 }
Пример #3
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();
        }