Код: Выделить всё
string directoryPathToWatch = ConfigurationManager.AppSettings["PathFile"]; // Path to directory that is being watched for newly created '.txt' files
private void fileSystemWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
Thread.Sleep(300000);
DirectoryInfo di = new DirectoryInfo(directoryPathToWatch);
FileSystemInfo[] files = di.GetFileSystemInfos("*.txt");
var dir = files.OrderBy(f => f.Name);
foreach (var file in dir)
{
// processes each file and after it is done, moves it to Output folder . . .
}
}
Насколько я понимаю, код также неверен, поскольку для каждый новый создан файл .txt в каталоге, создается новое событие Create event, которое обрабатывается в новом потоке(
Код: Выделить всё
fileSystemWatcher_CreatedЯ попробовал создать новые файлы почти одновременно, используя несколько потоков, используя Барьер, но испытание пройдено.
Код: Выделить всё
readonly string OUT_FOLDER_PATH = "D:\\path_to_directory_where_windows_service_Outputs_processed_files";
[Test]
public void Test_fsw_fails()
{
string sampleDataFolderPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "SampleData"); // folder where sample data is located
string[] inputDataFileNames = Enumerable.Range(0, 59)
.Select(n => $"{n.ToString("0000")}.txt")
.ToArray();
using (Process applicationProcess = new Process()
{
StartInfo = new ProcessStartInfo("D:\\path_to_windows_service_executable.exe")
{
CreateNoWindow = false
}
})
{
applicationProcess.Start();
Barrier moveInputDataBarrier = new Barrier(59);
Task[] moveInputDataTasks = Enumerable.Range(0, 59)
.Select(n => new Task(() =>
{
string localSampleDataFolderPath = sampleDataFolderPath;
string localFileName = inputDataFileNames[n];
int localN = n;
moveInputDataBarrier.SignalAndWait();
File.Copy(Path.Combine(localSampleDataFolderPath, localFileName), Path.Combine(OUT_FOLDER_PATH, localFileName));
}))
.ToArray();
applicationProcess.Kill();
}
Assert.That(Directory.EnumerateFiles(OUT_FOLDER_PATH).Count(), Is.EqualTo(59)); // Correct number of files processed
foreach (var actualOutputFileFullPath in Directory.EnumerateFiles(OUT_FOLDER_PATH, "*.txt"))
{
byte[] actualOutputFileContent = File.ReadAllBytes(actualOutputFileFullPath);
byte[] expectedOutputFileContent = File.ReadAllBytes(Path.Combine(sampleDataFolderPath, Path.GetFileName(actualOutputFileFullPath)));
Assert.That(actualOutputFileContent, Is.EquivalentTo(expectedOutputFileContent)); // Check for content equality
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... ncorrectly
Мобильная версия