What I meant was
If there are 100 files, 100 threads will be created to read the file and process the contents of the file,
When there are 100 files, one thread is turned every second, and no threads are generated for the remaining 99.
One thread for one file is running, and 99 are off.
If you have 100 files, you have to create 100 multi-threads.
If there are 100 files, what are the ways to process them simultaneously?
public static void CollectData(CancellationTokenSource cts)
{
Task.Run(async () =>
{
while (true)
{
try
{
string shareIp = IniFileHandler.GetPrivateProfile("MOVE_PATH", "PATH", string.Empty, iniFileName);
if (shareIp == null || shareIp == "")
{
log.Error(iniFileName + " 의 폴더명을 기입하시기 바랍니다.");
break;
}
FileRead read = new FileRead();
await read.getDataTableFromFile(shareIp);
}
catch (Exception ex)
{
log.Error(ex.Message);
}
finally
{
await Task.Delay(1000);
}
}
});
}
public async Task getDataTableFromFile(string path)
{
try
{
string successPath = IniFileHandler.GetPrivateProfile("SUCCESS_PATH", "PATH", string.Empty, iniFileName);
string failPath = IniFileHandler.GetPrivateProfile("FAIL_PATH", "PATH", string.Empty, iniFileName);
// string originalPath = IniFileHandler.GetPrivateProfile("SUCCESS_PATH", "PATH", string.Empty, iniFileName);
DirectoryInfo directoryInfo = new DirectoryInfo(path);
string fileName = string.Empty;
string withoutFileName = string.Empty;
DataTable dt = new DataTable();
FileInfo[] files = directoryInfo.GetFiles();
if (directoryInfo.GetFiles().Length > 0)
{
// Console.WriteLine(directoryInfo.GetFiles());
foreach (FileInfo file in files)
{
fileName = file.FullName;
withoutFileName = files[0].Name;
string fileExtension = Path.GetExtension(fileName);
string strLine = string.Empty;
string[] strAry = new string[] { };
FileStream fs = new FileStream(fileName, FileMode.Open, System.IO.FileAccess.Read, FileShare.Read);
try
{
using (StreamReader reader = new StreamReader(fs, UTF8Encoding.UTF8, true))
{
if (reader == null)
{
}
if (fileExtension == ".txt")
{
strLine = await reader.ReadLineAsync();
strAry = strLine.Split('\t');
}
else if (fileExtension == ".csv")
{
strLine = await reader.ReadLineAsync();
strAry = strLine.Split(',');
}
else if (fileExtension == ".Log")
{
strLine = await reader.ReadLineAsync();
strAry = strLine.Split('\t');
//Console.WriteLine(strAry);
}
foreach (string str in strAry)
{
dt.Columns.Add(str);
}
while (reader.Peek() >= 0)
{
//Console.WriteLine(sr.Peek());
if (fileExtension == ".txt")
{
strLine = await reader.ReadLineAsync();
strAry = strLine.Split('\t');
}
else if (fileExtension == ".csv")
{
strLine = await reader.ReadLineAsync();
strAry = strLine.Split(',');
}
else if (fileExtension == ".Log")
{
strLine = await reader.ReadLineAsync();
strAry = strLine.Split('\t');
//Console.WriteLine(strAry);
}
dt.Rows.Add(strAry);
}
if (reader.Peek() < 0)
{
}
if (reader.EndOfStream == true)
{
reader.Close();
if (InsertDb(dt, fileName, path, successPath, fileExtension) == true)
{
Console.WriteLine(fileName + " 이 " + successPath + " 으로 이동합니다.");
log.Info(fileName + " 이 " + successPath + " 으로 이동합니다.");
moveToSuccess(fileName);
}
else
{
Console.WriteLine(fileName + " 이 " + failPath + " 으로 이동합니다.");
log.Info(fileName + " 이 " + successPath + " 으로 이동합니다.");
moveToFail(fileName);
}
}
}
}
catch (Exception ex)
{
//if (fileName == "")
//{
// Console.WriteLine("폴더에 파일이 없습니다 , 파일을 기입해주세요.");
//}2
}
}
//fileName = files[0].FullName;
}
if (fileName == "")
{
Console.WriteLine("폴더에 파일이 없습니다 , 파일을 기입하세요.");
return;
}
//await Task.Delay(1000);
}
catch (Exception ex)
{
log.Error(ex.Message);
}
}
foreach (FileInfo file in files)?await Parallel.ForEach(files, file => {...});?