0
我編寫了代碼,但它看起來不太優雅和直觀。我現在嘗試重構它。你在我的代碼中看到任何反模式?反模式,重構C#代碼示例
我正在使用圖片。我從文件夾中獲取圖像,處理它並在此之後刪除。
Console.WriteLine("Press ESC to exit");
bool isEmptyFolderFlagSet = false;
while (true)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape)
{
appExited(null, EventArgs.Empty);
return;
}
List<string> images = new List<string>(System.Linq.Enumerable.Concat
(System.IO.Directory.GetFiles(sourcePath, "*.pdf"), System.IO.Directory.GetFiles(sourcePath, "*.tif")));
if (images.Count == 0 && !isEmptyFolderFlagSet)
{
Console.WriteLine("Waiting for image...");
isEmptyFolderFlagSet = true;
}
else
{
isEmptyFolderFlagSet = false;
foreach (string imagePath in images)
{
try
{
processing.ProcessingFile(imagePath);
System.IO.File.Delete(imagePath);
}
catch (System.IO.FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
}
}
}
Thread.Sleep(500);
}
這是一個輪詢循環。所以,而不是Thread.Sleep我會使用一個計時器並定義一個回調。 –
屬於http://codereview.stackexchange.com – jgauffin