Async Await - 需要一些指导

时间:2016-01-08 01:14:18

标签: c# multithreading async-await task directory-walk

我已尝试了许多不同的方法来实现这一点,我确信这不是连接异步/等待多线程的正确方法。这是我到目前为止所拥有的。这是我尝试进行异步的目录漫游器。我知道你没有看到任何异步或等待关键字,这是因为我没有成功,但这就是我想要做的。现在它在控制台应用程序中运行,但是一旦我得到一个工作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();
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以使用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();
        }
    }
} 

答案 1 :(得分:1)

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

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

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总结文件计数等)

注意:未经测试:)