I have written this code. It recursively creates folders in the web system by making REST Calls. So basically, it creates a folder for the root node, then gets all the child nodes and parallely and recursively calls itself. (for each child)
the only problem with the code is that if a node has too may children OR if the hierarchy is too deep, then I start getting "TaskCancellation" errors.
I have already tried increasing the timeout to 10 minutes.. but that does not solve the problem.
So my question is how can I start say 50 tasks, then wait for something to get freed and proceed only when there is an open slot in 50.
Currently I think my code is going on creating tasks without any limit as is flows through the hierarchy.
public async Task CreateSPFolder(Node node, HttpClient client, string docLib, string currentPath = null)
{
        string nodeName = Uri.EscapeDataString(nodeName);
        var request = new { __metadata = new { type = "SP.Folder" }, ServerRelativeUrl = nodeName };
        string jsonRequest = JsonConvert.SerializeObject(request);
        StringContent strContent = new StringContent(jsonRequest);
        strContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose");
        HttpResponseMessage resp = await client.PostAsync(cmd, strContent);                
        if (resp.IsSuccessStatusCode)
        {                    
            currentPath = (currentPath == null) ? nodeName : currentPath + "/" + nodeName;
        }
        else
        {
            string content = await resp.Content.ReadAsStringAsync();
            Console.WriteLine(content);
            throw new Exception("Failed to create folder " + content);
        }
    }
    List<Task> taskList = new List<Task>();
    node.Children.ToList().ForEach(c => taskList.Add(CreateSPFolder(c, client, docLib, currentPath)));
    Task.WaitAll(taskList.ToArray());
}