异步等待 - 需要一些指导

Async Await - Need some guidance

我已经尝试了许多不同的方法来让它工作,我确信这不是为多线程连接 async/await 的正确方法。这是我到目前为止所拥有的。这是我试图异步的目录 walker。我知道您没有看到任何 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();
            }
        }
    }

您可以 运行 同步代码与 Task.Run(() => { //code }); 异步 同时将您的 Return 类型更改为 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();
        }
    }
} 

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

与其在你的方法中做副作用,不如尝试返回收集到的值。例如

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 等对您的文件计数求和)

注意:未经测试:)