Memory space error with C #, using FileWatcher, MODI threads and queues

0

I am putting OCR images in .tif format with the SharePoint 2007 dll (MODI).

I analyze with FileWatcher a folder including subdirectories, the alert is generated when new files are created inside the structure, I keep the path of the new file in a Queue type object,

Subsequently activates a thread to go through each route to a method that saves the recognized text in a .txt file, when processing 20 to 50 files it does it well then it throws me: ExtractTextFromImage () = > Error: Not enough storage is available to complete this operation.

The previous error is in an environment tests in windows server 2008, but when doing so in Windows 10 I add 1000 images and process 999 and the error is: OCR running error.

Is the library not ready to run those kinds of charges? or is it wrong to precede the files? Is there another way to monitor the folders ?, the structure is fixed

// Event is triggered by FileWatcher when new .tif files are detected

    public void EncolarPath(object source, FileSystemEventArgs e)
    {
        EntradaTiff(e.FullPath);

    }

// This is the thread method:

    public void EntradaTiff(string filepath)
    {
        Cola.Enqueue(filepath);

        if (HiloTrabajo == null)
        {
            HiloTrabajo = new Thread(new ThreadStart(Procesar));
            HiloTrabajo.Start();
        }
        else if (HiloTrabajo.ThreadState == ThreadState.WaitSleepJoin)
        {
            Sicronizacion.Set();
        }
    }

// Method that runs after enabling the thread:

    private void Procesar()
    {
        while (true)
        {
            string filepath = RecuperarArchivo();

            if (filepath != null)
            {
                ExtractTextFromImage(filepath);
            }

            else
                Sicronizacion.WaitOne();
        }
    }

// Extract the file from the queue

    private string RecuperarArchivo()
    {
        if (Cola.Count > 0)
            return Cola.Dequeue();
        else
            return null;
    }

// This method receives the path of the file to reach the recognized text // and save the text in a file in plain format (.txt)

    private void ExtractTextFromImage(string filePath)
    {
        string extractedText = "";
        string text = String.Empty;

        try
        {
            Document modiDocument = new Document();
            Console.WriteLine("ExtractTextFromImage() => Traza :" + filePath + " Linea : " + 1);
            GC.Collect();
            Console.WriteLine("ExtractTextFromImage() => Traza :" + filePath + " Linea : " + 2);
            modiDocument.Create(filePath); 
            Console.WriteLine("ExtractTextFromImage() => Traza :" + filePath + " Linea : " + 3);
            try    //Esta dentro de try catch por que al principio tronaba en esta linea
            {
                modiDocument.OCR(MiLANGUAGES.miLANG_SPANISH);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ExtractTextFromImage() => Error :" + ex.Message);
            }
            Console.WriteLine("ExtractTextFromImage() => Traza :" + filePath + " Linea : " + 4);
            MODI.Image modiImage = (modiDocument.Images[0] as MODI.Image);
            Console.WriteLine("ExtractTextFromImage() => Traza :" + filePath + " Linea : " + 5);
            extractedText = modiImage.Layout.Text;
            Console.WriteLine("ExtractTextFromImage() => Traza :" + filePath + " Linea : " + 6);
            modiDocument.Close();

            extractedText = extractedText.Replace("<", "").Replace("&", "").Replace(">", "");
            contenido.Add(extractedText.Replace(Environment.NewLine, "\r\n"));
            SaveTxt(contenido, filePath);
            contenido.Clear();
            Console.WriteLine("ExtractTextFromImage() => Traza :" + filePath + " Tiene contenido : " + extractedText.Length);
            extractedText = String.Empty;
            modiDocument = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        catch (Exception ex)
        {

            Console.WriteLine("ExtractTextFromImage() => Error :" + ex.Message);
            Console.WriteLine("ExtractTextFromImage() => Error :" + modiDocument.ToString());


        }
    }
    
asked by Angel Martin Benitez Jimenez 27.07.2018 в 21:33
source

0 answers