Пример #1
0
        public async Task ExtractFiles(string directoryName, IFileSystem fileSystem, ExtractionProgressedToken progressReportToken = null)
        {
            if (progressReportToken != null)
            {
                progressReportToken.TotalFileCount = Headers.Count(h => h != null && !string.IsNullOrEmpty(h.Filename));
            }

            if (!fileSystem.DirectoryExists(directoryName))
            {
                fileSystem.CreateDirectory(directoryName);
            }

            var a = new AsyncFor();
            await a.RunForEach(Headers, async (header) =>
            {
                if (string.IsNullOrEmpty(header.Filename))
                {
                    return;
                }

                fileSystem.WriteAllBytes(Path.Combine(directoryName, header.Filename), await ExeFsData.ReadArrayAsync(0x200 + header.Offset, header.FileSize));

                if (progressReportToken != null)
                {
                    progressReportToken.IncrementExtractedFileCount();
                }
            });
        }
        public async Task ThrowsOnSimultaneousInstanceUsage()
        {
            var sampleData = Enumerable.Repeat(new TestClass(), 5);

            var f         = new AsyncFor();
            var firstTask = f.RunForEach(sampleData, async data =>
            {
                data.Success = true;
                await Task.Delay(100);
            });

            await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await f.RunForEach(sampleData, async data =>
                {
                    data.Success = true;
                    await Task.Delay(100);
                });
            });
        }
        public async Task RunsForEveryItemInGenericEnumerableWithSynchronousDelegate_InstanceMethod()
        {
            var sampleData = Enumerable.Repeat(new TestClass(), 5);

            var f = new AsyncFor();
            await f.RunForEach(sampleData, data =>
            {
                data.Success = true;
            });

            Assert.All(sampleData, data => Assert.True(data.Success));
        }
        public async Task ReportsProgressThroughInstanceEvent()
        {
            var myCustomMessage   = $"Loading... ({Guid.NewGuid()})";
            var progressEventArgs = new ConcurrentBag <ProgressReportedEventArgs>();
            var completedCount    = 0;

            void OnProgressChanged(object sender, ProgressReportedEventArgs e)
            {
                progressEventArgs.Add(e);
                Assert.Equal(myCustomMessage, e.Message);
                Assert.False(e.IsIndeterminate);
                Assert.True(e.Progress <= 1);
            }

            void OnCompleted(object sender, EventArgs e)
            {
                Interlocked.Increment(ref completedCount);
            }

            var sampleData = Enumerable.Repeat(new TestClass(), 5).ToList();

            var f = new AsyncFor();

            f.Message          = myCustomMessage;
            f.ProgressChanged += OnProgressChanged;
            f.Completed       += OnCompleted;
            f.BatchSize        = 2;
            await f.RunForEach(sampleData, data =>
            {
                data.Success = true;
            });

            f.ProgressChanged -= OnProgressChanged;
            f.Completed       -= OnCompleted;


            Assert.Equal(sampleData.Count, progressEventArgs.Count);
            Assert.Equal(1, completedCount);

            var distinctProgressPercentages = progressEventArgs.Select(e => e.Progress).Distinct().ToList();

            Assert.InRange(distinctProgressPercentages.Count, 2, 5);
            Assert.All(distinctProgressPercentages, p => Assert.InRange(p, 0, 1));
            Assert.Contains(distinctProgressPercentages, p => p != 0 && p != 1);
        }
Пример #5
0
        public async Task <IEnumerable <FileTypeDetectionResult> > DetectFileType(GenericFile file, PluginManager manager)
        {
            ConcurrentQueue <FileTypeDetectionResult> matches = new ConcurrentQueue <FileTypeDetectionResult>();
            AsyncFor f = new AsyncFor();

            f.RunSynchronously = !file.IsThreadSafe;
            await f.RunForEach(manager.GetRegisteredObjects <IDetectableFileType>(), async (x) =>
            {
                if (await x.IsOfType(file))
                {
                    matches.Enqueue(new FileTypeDetectionResult {
                        FileType = x.GetType().GetTypeInfo(), MatchChance = 0.5f
                    });
                }
            });

            return(matches);
        }
        public async Task ReportsProgressThroughInstanceProperties()
        {
            var sampleData = Enumerable.Repeat(new TestClass(), 5);

            var f = new AsyncFor();
            await f.RunForEach(sampleData, data =>
            {
                Assert.False(f.IsCompleted);
                Assert.False(f.IsIndeterminate);
                Assert.True(f.Progress < 1);

                data.Success = true;
            });

            Assert.True(f.IsCompleted);
            Assert.False(f.IsIndeterminate);
            Assert.Equal(1, f.Progress, precision: 1);

            Assert.All(sampleData, data => Assert.True(data.Success));
        }
Пример #7
0
        /// <summary>
        /// Asynchronously copies a directory
        /// </summary>
        /// <param name="sourceDirectory">The directory to copy</param>
        /// <param name="destinationDirectory">The new destination for the source directory</param>
        public static async Task CopyDirectory(string sourceDirectory, string destinationDirectory, IFileSystem provider)
        {
            // Get the files/directories to copy
            var files = provider.GetFiles(sourceDirectory, "*", false);

            //Create all required directories
            foreach (var item in files)
            {
                var dest = item.Replace(sourceDirectory, destinationDirectory);
                if (!provider.DirectoryExists(Path.GetDirectoryName(dest)))
                {
                    provider.CreateDirectory(Path.GetDirectoryName(dest));
                }
            }

            AsyncFor f = new AsyncFor();

            f.RunSynchronously = false;
            await f.RunForEach(files, path =>
            {
                string dest = path.Replace(sourceDirectory, destinationDirectory);
                provider.CopyFile(path, dest);
            }).ConfigureAwait(false);
        }