V
Vagram
Hi All,
I am developing an automated application that uses Microsoft Office Document
Imaging (MODI) to perform OCR on a large number of TIFF files.
In the code, I have a loop that goes through all the tiff files that need to
be processed. On each run of the loop I call a function (see below) which
creates a new MODI.Document COM object to OCR the TIFF File. At the end of
the function I release the COM object.
Occasionally the automated application encounters one of the following issues:
1.Sometimes the TIFF file is not released by the OCR process, thus it can’t
be moved or deleted until the application closes.
2.When performing OCR on an image object, sometimes a RPC_E_SERVERFAULT
(code: -2147417851) exception is thrown.
When the application performs OCR on 60 TIFF files, usually 1 random file
will not be released when Marshal.FinalReleaseComObject is called.
How can I modify the code so that the errors mentioned above do not occur?
private void SimpleOCR(FileInfo objFileName)
{
try
{
//SetImage
m_objMODIDocument = new MODI.Document();
m_objMODIDocument.Create(objFileName.FullName);
try //Perform OCR
{
for (int i = 0; i < m_objMODIDocument.Images.Count; ++i)
{
MODI.IImage objImage = null;
try
{
objImage =
(MODI.IImage)(m_objMODIDocument.Images);
objImage.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH,
true, true);
}
catch
{
LogFile.WriteLine("Cannot OCR page " + (i + 1));
}
finally
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objImage);
objImage = null;
}
}
LogFile.WriteLine(objFileName.FullName + ", OCR OK! ");
}
catch (Exception ex)
{
LogFile.WriteLine(objFileName.FullName + ", OCR FAIL! EX
= " + ex.Message);
}
m_objMODIDocument.Save(); //(This causes the object not to
get relased some times)
System.Threading.Thread.Sleep(1000);
m_objMODIDocument.Close(false);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(m_objMODIDocument);
for (int i = 0; i < 5; ++i)
{
if (move(objFileName))
break;
else
{
System.Threading.Thread.Sleep(1000); // if cannot
move the document sleep and try again
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(m_objMODIDocument);
GC.Collect();
GC.WaitForPendingFinalizers();
}
if (i == 4)
{
LogFile.WriteLine("The file was not released.");
}
}
m_objMODIDocument = null;
}
catch (Exception ex)
{
LogFile.WriteLine("The file was skipped. EX: " + ex.Message);
}
}
public bool move(FileInfo objFileName)
{
try //DeleteMoveFiles(objFileName, true);
{
System.IO.File.Move(objFileName.FullName,
DocScan.Properties.Settings.Default.OCRMoveTifsFolder + "\\" +
objFileName.Name);
LogFile.WriteLine(objFileName.FullName + " was successfully
moved");
return true;
}
catch (Exception ex)
{
LogFile.WriteLine("*** WARNING: Could not Move or Delete
file. (" + ex.Message + ") ");
return false;
}
}
Thanks,
I am developing an automated application that uses Microsoft Office Document
Imaging (MODI) to perform OCR on a large number of TIFF files.
In the code, I have a loop that goes through all the tiff files that need to
be processed. On each run of the loop I call a function (see below) which
creates a new MODI.Document COM object to OCR the TIFF File. At the end of
the function I release the COM object.
Occasionally the automated application encounters one of the following issues:
1.Sometimes the TIFF file is not released by the OCR process, thus it can’t
be moved or deleted until the application closes.
2.When performing OCR on an image object, sometimes a RPC_E_SERVERFAULT
(code: -2147417851) exception is thrown.
When the application performs OCR on 60 TIFF files, usually 1 random file
will not be released when Marshal.FinalReleaseComObject is called.
How can I modify the code so that the errors mentioned above do not occur?
private void SimpleOCR(FileInfo objFileName)
{
try
{
//SetImage
m_objMODIDocument = new MODI.Document();
m_objMODIDocument.Create(objFileName.FullName);
try //Perform OCR
{
for (int i = 0; i < m_objMODIDocument.Images.Count; ++i)
{
MODI.IImage objImage = null;
try
{
objImage =
(MODI.IImage)(m_objMODIDocument.Images);
objImage.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH,
true, true);
}
catch
{
LogFile.WriteLine("Cannot OCR page " + (i + 1));
}
finally
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objImage);
objImage = null;
}
}
LogFile.WriteLine(objFileName.FullName + ", OCR OK! ");
}
catch (Exception ex)
{
LogFile.WriteLine(objFileName.FullName + ", OCR FAIL! EX
= " + ex.Message);
}
m_objMODIDocument.Save(); //(This causes the object not to
get relased some times)
System.Threading.Thread.Sleep(1000);
m_objMODIDocument.Close(false);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(m_objMODIDocument);
for (int i = 0; i < 5; ++i)
{
if (move(objFileName))
break;
else
{
System.Threading.Thread.Sleep(1000); // if cannot
move the document sleep and try again
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(m_objMODIDocument);
GC.Collect();
GC.WaitForPendingFinalizers();
}
if (i == 4)
{
LogFile.WriteLine("The file was not released.");
}
}
m_objMODIDocument = null;
}
catch (Exception ex)
{
LogFile.WriteLine("The file was skipped. EX: " + ex.Message);
}
}
public bool move(FileInfo objFileName)
{
try //DeleteMoveFiles(objFileName, true);
{
System.IO.File.Move(objFileName.FullName,
DocScan.Properties.Settings.Default.OCRMoveTifsFolder + "\\" +
objFileName.Name);
LogFile.WriteLine(objFileName.FullName + " was successfully
moved");
return true;
}
catch (Exception ex)
{
LogFile.WriteLine("*** WARNING: Could not Move or Delete
file. (" + ex.Message + ") ");
return false;
}
}
Thanks,