I am using Tasks to do some processes. The tasks will be in a loop, I am not sure sure how many tasks/iterations will be there. It will vary time to time. How to use the Tasks? Below is my code.
void func1(string loc)
{
var CurrentDirectoryInfo = new DirectoryInfo(loc);
Task[] tasks; // null
int index = 0;
foreach (DirectoryInfo D1 in CurrentDirectoryInfo.GetDirectories)
{
tasks[index] = Task.Factory.StartNew(() =>func1(d1.FullName));
index++;
}
If I use null for the Task[] tasks, I am getting Object reference not set to an instance of an object error.
If I leave it unassigned, I am getting Use of unassigned variable error.
StartNewand also decide whether you need an individual task created per directory/subdirectory. Chances are... you don't (how many cores does your PC have?).taskswere any other data type, and you were trying to access an instance method/property?