There is a class for the implementation of FTP client with synchronization.

class FtpFile { private string md5; private string file; private string path; private string status; public string File { get { return file; } set { file = value; } } public string Status { get { return status; } set { status = value; } } public string Md5 { get { return md5; } set { md5 = value; } } public string Path { get { return path; } set { path = value; } } 

I want it to go on when I press a button in an infinite loop. I describe the алгоритм

  1. Check file availability. If nobody uses the file, go ahead

  2. Check the md5 file. If it is different, then we will load the file on the server

     var listFtpFile = new List<FtpFile>(); FtpFile ftpFile; for (int i = 0; i < dgv.Rows.Count; i++) { ftpFile = new FtpFile(); ftpFile.File = dgv.Rows[i].Cells[1].Value.ToString(); // файл 123.txt ftpFile.Path = dgv.Rows[i].Cells[0].Value.ToString(); // путь / listFtpFile.Add(ftpFile); /* if (Helper.IsFileReady(file)) { var md5 = Helper.CalculateMD5Hash(file); if (ftpFile.Md5 != md5) { ftpFile.Md5 = Helper.CalculateMD5Hash(file); myFTP myFtp = new myFTP(host, login, password, "/"); } } */ } 

Now the question is how to correctly implement an infinite loop for Parallel.For . I do not know, maybe there is a better design just on Thread. Tell me where to swim.

    1 answer 1

     public class ParallelUtils { public static void While(Func<bool> condition, Action body) { Parallel.ForEach(IterateUntilFalse(condition), ignored => body()); } private static IEnumerable<bool> IterateUntilFalse(Func<bool> condition) { while (condition()) yield return true; } } 

    We use this:

     Func<bool> whileCondFn = () => !_requestStopFlag; ParallelUtils.While(whileCondFn, delegate() { }); 

    Source: Implementing Parallel While with Parallel.ForEach