异步等待-需要一些指导



我已经尝试了许多不同的方法来实现这一点,我确信这不是为多线程连接async/await的正确方法。这是我迄今为止所拥有的。这是一个目录助行器,我试图使其异步。我知道你看不到任何异步或等待关键字,这是因为我没有成功,但这正是我正在努力做的。现在它在控制台应用程序中运行,但我稍后会在得到一个有效的POC后进行抽象和重构。欢迎提供任何指导。

    static void RunProgram(CancellationToken ct)
    {
        try
        {
            foreach (var dir in _directoriesToProcess)
            {
                var newTask = CreateNewTask(dir, ct);
                _tasks.Add(newTask);
            }
            while (_tasks.Count > 0)
            {
                lock (_collectionLock)
                {
                    var t = _tasks.Where(x => x.IsCompleted == true).ToList();
                    if (t != null)
                        foreach (var task in t)
                        {
                            _tasks.Remove(task);
                        }
                }
            }
            OutputFiles();
            StopAndCleanup();
        }
        catch (Exception ex)
        {
            Log(LogColor.Red, "Error: " + ex.Message, false);
            _cts.Cancel();
        }
    }

    static Task CreateNewTask(string Path, CancellationToken ct)
    {
        return Task.Factory.StartNew(() => GetDirectoryFiles(Path, ct), ct);
    }
    static void GetDirectoryFiles(string Path, CancellationToken ct)
    {
        if (!ct.IsCancellationRequested)
        {
            List<string> subDirs = new List<string>();
            int currentFileCount = 0;
            try
            {
                currentFileCount = Directory.GetFiles(Path, _fileExtension).Count();
                subDirs = Directory.GetDirectories(Path).ToList();
                lock (_objLock)
                {
                    _overallFileCount += currentFileCount;
                    Log(LogColor.White, "- Current path: " + Path);
                    Log(LogColor.Yellow, "--  Sub directory count: " + subDirs.Count);
                    Log(LogColor.Yellow, "--  File extension: " + _fileExtension);
                    Log(LogColor.Yellow, "--  Current count: " + currentFileCount);
                    Log(LogColor.Red, "--  Running total: " + _overallFileCount);
                    _csvBuilder.Add(string.Format("{0},{1},{2},{3}", Path, subDirs.Count, _fileExtension, currentFileCount));
                    Console.Clear();
                    Log(LogColor.White, "Running file count: " + _overallFileCount, false, true);
                }
                foreach (var dir in subDirs)
                {
                    lock (_collectionLock)
                    {
                        var newTask = CreateNewTask(dir, ct);
                        _tasks.Add(newTask);
                    }
                }
            }
            catch (Exception ex)
            {
                Log(LogColor.Red, "Error: " + ex.Message, false);
                _cts.Cancel();
            }
        }
    }

我认为您尝试做的事情没有任何问题,只是要小心不受控制的并发,例如在不同的线程上同时读取太多目录。上下文切换最终可能会使速度变慢。

不要在方法中做副作用,而是尝试返回收集的值。例如

static async Task<IEnumerable<DirectoryStat>> GetDirectoryFiles(string path, string fileExtension, CancellationToken ct)
{
    var thisDirectory = await Task.Run(() => /* Get directory file count and return a DirectoryStat object */);
    var subDirectoriesResults = await Task.WhenAll(Directory.GetDirectories(path).Select(dir => GetDirectoryFiles(dir, fileExtension, ct)));
    return (new[] { thisDirectory }).Concat(subDirectoryResults);
} 

然后,您可以稍后对它们进行迭代,并从DirectoryStat中提取所需的数据(并根据_overallFileCount等对文件计数求和)

注:未测试:)

您可以使用Task.Run(() => { //code });运行同步代码异步同时将您的退货类型更改为Task,以便您可以将其更改为await我会重写你的代码如下:

static void RunProgram(CancellationToken ct)
{
    try
    {
        foreach (var dir in _directoriesToProcess)
        {
            var newTask = CreateNewTask(dir, ct);
            _tasks.Add(newTask);
        }
        //change your while so it does not execute all the time
        while (_tasks.Count > 0)
        {
            lock (_collectionLock)
            {
                var tsk = _tasks.FirstOrDefault();
                        if (tsk != null)
                        {
                            if (tsk.Status <= TaskStatus.Running)
                                await tsk;
                            _tasks.Remove(tsk);
                        }
            }
        }
        OutputFiles();
        StopAndCleanup();
    }
    catch (Exception ex)
    {
        Log(LogColor.Red, "Error: " + ex.Message, false);
        _cts.Cancel();
    }
}

static Task CreateNewTask(string Path, CancellationToken ct)
{
    return Task.Factory.StartNew(() => GetDirectoryFiles(Path, ct), ct);
}
//always use Task (or Task<T>) as return so you can await the process
static async Task GetDirectoryFiles(string Path, CancellationToken ct)
{
    if (!ct.IsCancellationRequested)
    {
        //Insert Magic
        await Task.Run(() => {
            List<string> subDirs = new List<string>();
            int currentFileCount = 0;
            try
            {
                currentFileCount = Directory.GetFiles(Path, _fileExtension).Count();
                subDirs = Directory.GetDirectories(Path).ToList();
                lock (_objLock)
                {
                    _overallFileCount += currentFileCount;
                    Log(LogColor.White, "- Current path: " + Path);
                    Log(LogColor.Yellow, "--  Sub directory count: " + subDirs.Count);
                    Log(LogColor.Yellow, "--  File extension: " + _fileExtension);
                    Log(LogColor.Yellow, "--  Current count: " + currentFileCount);
                    Log(LogColor.Red, "--  Running total: " + _overallFileCount);
                    _csvBuilder.Add(string.Format("{0},{1},{2},{3}", Path, subDirs.Count, _fileExtension, currentFileCount));
                    Console.Clear();
                    Log(LogColor.White, "Running file count: " + _overallFileCount, false, true);
                }
                foreach (var dir in subDirs)
                {
                    lock (_collectionLock)
                    {
                        var newTask = CreateNewTask(dir, ct);
                        _tasks.Add(newTask);
                    }
                }
            });
        }
        catch (Exception ex)
        {
            Log(LogColor.Red, "Error: " + ex.Message, false);
            _cts.Cancel();
        }
    }
} 

相关内容

  • 没有找到相关文章

最新更新