MODI – OCR – (Error Releasing COM object & RPC_E_SERVERFAULT)

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,
 
A

Andrei Smolin [Add-in Express]

Hi Vagram,

I guess m_objMODIDocument.Images returns a COM object. If so, you need to
save it to a separate variable and release it after use.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com

Vagram said:
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,
 
V

Vagram

Thanks For your help. After some further investigation i had found that
another component (not mentioned here) was accessing the TIFF files. The
controls behavior caused the errors that were described.



Andrei Smolin said:
Hi Vagram,

I guess m_objMODIDocument.Images returns a COM object. If so, you need to
save it to a separate variable and release it after use.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com

Vagram said:
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,

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top