My delete call does not work because the files in the directory are read only (still being accessed) when called. How do I wait until the file has been processed and released by the tesseract engine to delete it?
I have tried a while loop at the "delete" call waiting for an exception NOT to be thrown - this just hangs. I've seen examples using Tasks, Threads, and Processes. What it is programmatically the right way to do this?
// create a subdirectory called processing
Directory.CreateDirectory(dir + "//processing");
string directory = dir + "//processing";
// extract the images to the processing directory
ImageExtractor.ExtractImagesFromFile(LongName, "img", directory, true);
// extract the text from all the files in processing
string[] images = Directory.GetFiles(directory);
foreach (string file in images)
{
Bitmap image = new Bitmap(file);
using (TesseractEngine engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
{
using(var img = PixConverter.ToPix(image))
{
using(var page = engine.Process(img))
{
fileContent = fileContent + " " + page.GetText();
}
}
}
}
Directory.Delete(dir + "//processing", true);
Using a task... (does not work for me)...
Task t = Task.Run(() => {
foreach (string file in images)
{
Bitmap image = new Bitmap(file);
using (TesseractEngine engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
{
using(var img = PixConverter.ToPix(image))
{
using(var page = engine.Process(img))
{
fileContent = fileContent + " " + page.GetText();
}
}
}
}
});
t.Wait();
Directory.Delete(dir + "//processing", true);