Beispiel #1
0
            public async Task <List <string> > IterateFolders(string folderName, string searchString, CancellationToken token)
            {
                var foundList = new List <string>();

                if (!Directory.Exists(folderName))
                {
                    OnIterate?.Invoke(new MonitorProgressArgs("Folder does not exists!"));
                    _folderFound = false;
                    return(foundList);
                }

                _folderFound = true;
                var currentFileName = "";

                try
                {
                    foreach (var fileName in Directory.EnumerateFiles(folderName, "*.txt", SearchOption.AllDirectories))
                    {
                        currentFileName = fileName;
                        OnIterate?.Invoke(new MonitorProgressArgs(fileName));

                        var contents = File.ReadAllText(fileName);

                        await Task.Delay(1, token);

                        if (contents.Contains(searchString))
                        {
                            foundList.Add(fileName);
                        }

                        if (token.IsCancellationRequested)
                        {
                            token.ThrowIfCancellationRequested();
                        }
                    }
                }
                catch (Exception ex)
                {
                    OnIterate?.Invoke(new MonitorProgressArgs($"Access denied {currentFileName}"));
                }

                return(foundList);
            }
Beispiel #2
0
        /// <summary>
        /// Run a long running task composed of small awaited and configured tasks
        /// Using ConfigureAwait(false) makes this particular method run a bit faster
        /// then the example above.
        /// </summary>
        public async Task <int> ProcessAsync2(int loop, CancellationToken ct)
        {
            int index = 0;

            for (int i = 0; i < loop; i++)
            {
                Console.WriteLine(SynchronizationContext.Current == null);
                await Task.Delay(10, ct).ConfigureAwait(false);

                OnIterate?.Invoke(this, new ProcessIndexingArgs(index));

                if (ct.IsCancellationRequested)
                {
                    ct.ThrowIfCancellationRequested();
                }

                index += i;
            }

            return(index);
        }
Beispiel #3
0
        ///  <summary>
        ///  In this example each time await is called we go back to the captured
        ///  context after the execution of the awaited task.
        ///
        ///  Calling ConfigureAwait(false) after the task means that we do not care
        ///  if the code after the await, runs on the captured context or not.
        ///  </summary>
        ///  <param name="loop"></param>
        /// <param name="ct">Valid cancellation token</param>
        /// <returns></returns>
        public async Task <int> ProcessAsync1(int loop, CancellationToken ct)
        {
            var result = 0;

            for (var index = 0; index < loop; index++)
            {
                Console.WriteLine(SynchronizationContext.Current == null);
                await Task.Delay(10, ct);

                OnIterate?.Invoke(this, new ProcessIndexingArgs(index));

                if (ct.IsCancellationRequested)
                {
                    ct.ThrowIfCancellationRequested();
                }

                result += index;
            }

            return(result);
        }