Search code examples
c#asp.netmultithreadingparallel-processingparallel.foreach

How I can run multiple processes parallel but inside each process execution sequential


I am working on CSV files in my code I just download 5 files from ftp server and then process these files. I want to download all 5 files first and then start processing.

it is about products against every product there are 5 files, I actually want parallel execution for products but sequential execution for downloading and processing them. I did it with Parallel.ForEach but it doesn't work for me it download 3 files and start processing or download 2 files and start processing and then download remaining files.

I simply want to download all files first then start processing them, but parallel processing between products

Here is Parallel.ForEach (calling method)

Parallel.ForEach(dt.AsEnumerable(), row =>
                {
                    //code here 
                    Products(eSetting, row);
                });

Actual method that download and process files:

public static void Products(ETLServiceSettings eSetting, DataRow row)
{

    IEnumerable<string> Datafiles = GetFilesInFtpDirectory("ftp://");
    foreach (string file in Datafiles)
    {
        string file_Name = "";

        if (file.ToLower().Contains(".csv"))
        {
            productId = Convert.ToInt32(row["ProductId"].ToString());

            file_Name = DownloadFile("ftp://");
        }
    }

    string targetDirectory = Environment.CurrentDirectory + "\\" +
        ConfigurationManager.AppSettings["InProcessETLFiles"].ToString() + "\\";

    List<string> vfiles = new List<string>(), sfiles = new List<string>(),
        ufiles = new List<string>(), psfiles = new List<string>(),
        sufiles = new List<string>();

    foreach (var fileEntries in Directory.GetFiles(targetDirectory))
    {
        //Files processing code is here
        FileInfo info = new FileInfo(fileEntries);
        var fileName = Path.GetFileName(info.FullName);
    }
}

First the foreach on top should be complete and then execute the next foreach for processing files.


Solution

  • Split up the Programs function into the downloading component and the processing component. Do your downloads in the parallel for each, then call your processing function. Parallel for each returns when all processes are complete.