My program consists of the following steps:
1. read in any new image from a source folder
2. process image (detect objects and deskew them)
3. save deskewed object images in a target folder
...repeat
The idea is that while scanning stamps the program outputs each stamp individually or grouped if the stamps are clustered. The user should be able to continuously make scans while they are being processed.
The program behaves in three distinct ways, based on three ways of detecting new images in the target folder. In practice only the third way will be used. The first two can hopefully help explain what is happening and why.
1. Drag-and-drop
All works fine when drag-and-dropping an image into the folder manually; I can do this indefinitely and the outputs are correct.
2. Copy paste
When copy and pasting an image locally in the target folder the program halts immediately, showing following exception:
System.IO.IOException: 'Unable to access the file xxx because it is being used by another process.'
This same exception occurs when copy-pasting images within the target folder.
3. Output from scan program
I use the ScanSnap SV600 which saves unprocessed scans of stamps in the target folder. Now here comes the case that baffles me: my processing program alternates in its behaviour. The first scan always succeeds every time I start the program in Debug mode. But reading in the subsequent scan within that same run of the processing program fails in the case when the previous Debug session succeeded. In other words: one out of two times the program behaves as desired. And the other times it halts on the second scan, giving me the above error.
To detect new images I use:
var fileSystemWatcher = new FileSystemWatcher(@inputDir)
{
Filter = "*.jpg",
NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite,
EnableRaisingEvents = true
};
fileSystemWatcher.Created += new FileSystemEventHandler(OnFileCreated);
What I have tried:
Furthermore this is part of the code before processing begins. It is not the most beautiful and feels a bit 'hacky' using `Thread.Sleep()`. Maybe here is a clue?
private void OnFileCreated(object sender, FileSystemEventArgs e)
{
sw.Restart();
string imagePath = e.FullPath;
inputFiles.AddToQ(imagePath);
Console.WriteLine("{0} is queued.", e.FullPath);
int number = 0;
if (!int.TryParse(setCluster.Text.Trim(), out number))
{
MessageBox.Show("Please enter a valid number for Clustering.");
setCluster.Text = "20";
}
else
{
while (inputFiles.Inspect() != imagePath) Thread.Sleep(100);
try
{
lock (locker)
{
Bitmap incoming = new Bitmap(inputFiles.Process());
Console.WriteLine("{0} is being processed.", e.FullPath);
if (currentScan.Image != null) currentScan.Image = null;
currentScan.Invoke((Action)
delegate ()
{
currentScan.Image = (Image)new Bitmap(e.FullPath);
currPathText.Text = imagePath;
});
Bitmap resized = new Bitmap(incoming, new
Size(Convert.ToInt32(incoming.Width / Variables.ScalingFact),
Convert.ToInt32(incoming.Height / Variables.ScalingFact)));
.....