I have a problem I can not understand how to create threads within a cycle and that they all run simultaneously, and when all the threads are finished, send a message.
I have this code:
for (int i = 0; i <= x - 1; i++)
{
if (File.Exists(fileList[i]))//valida que exista el archivo
{
FileInfo finfo = new FileInfo(fileList[i]);
if (finfo.Extension == ".ctl") //valida que solo sean CTL
{
ExecuteBatFile(path, finfo.Name);//ejecuta proceso
}
}
}
and it is the procedure that executes the .batch file
// proceso para ejecutar un archivo .batch
public void ExecuteBatFile(string _path, string _archivoCTL)
{
CrearBatch(_path, _archivoCTL);
Process proc = null;
try
{
string targetDir = string.Format(_path); //this is where mybatch.bat lies
proc = new Process();
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = _archivoCTL.Substring(0, _archivoCTL.Length - 4) + ".bat";
proc.StartInfo.Arguments = string.Format("10"); //this is argument
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //this is for hiding the cmd window...so execution will happen in back ground.
proc.Start();
proc.WaitForExit();
}
catch (Exception ex)
{
MessageBox.Show("Exception Occurred :{0},{1}", ex.Message);
}
}
My question is where to create the threads so that they are executed simultaneously (so that the ExecuteBatFile is executed) and go checking if they are all finished.
Thank you very much for your advice. !!!