McVrsServerMutexMgr error when asynchronous scanning
Posted: Fri May 31, 2019 4:10 pm
Hello,
Recently, our Document Scanning departement request us to be able to add new page to a box in a C# Tools.
So the process is to scan by asynchronous background worker a batch of documents, work on it and add some other document in a second time.
The C# Tools use the recommended way of Doing things.
private BackgroundWorker _backGroundWorker = new System.ComponentModel.BackgroundWorker();
_backGroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(m_BackgroundWorker_DoWork);
_backGroundWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(m_BackgroundWorker_ProgressChanged);
public void m_BackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
try
{
IntPtr hwnd = IntPtr.Zero;
if (_gdPictureImaging.TwainOpenDefaultSource(hwnd.ToInt32()))
{
int scanCount = 0;
_gdPictureImaging.TwainSetHideUI(scanProperty.chkHideGUI.Checked);
_gdPictureImaging.TwainSetModalUI(scanProperty.chkModalUI.Checked);
_gdPictureImaging.TwainSetIndicators(true);
if (_gdPictureImaging.TwainIsFeederSelected())
{
_gdPictureImaging.TwainSetAutoFeed(true);
_gdPictureImaging.TwainSetAutoScan(true);
}
do
{
if (!_backGroundWorker.CancellationPending)
{
int imageId = _gdPictureImaging.TwainAcquireToGdPictureImage(hwnd.ToInt32());
if (imageId != 0)
{
scanCount++;
_backGroundWorker.ReportProgress(scanCount, imageId);
}
}
else
{
break;
}
} while (_gdPictureImaging.TwainGetState() > TwainStatus.TWAIN_SOURCE_ENABLED);
if (scanCount > 0)
{
Outils.log.Info("Numérisation finie, " + scanCount + " page(s) acquired");
}
else
{
MessageBox.Show(this, "Le scan a échoué, veuillez consulter les logs.");
}
_gdPictureImaging.TwainCloseSource();
}
else
{
MessageBox.Show("Unable to open the default TWAIN source. " + '\r' + "Result code: " + _gdPictureImaging.TwainGetLastResultCode() +
'\r' + "Condition code: " + _gdPictureImaging.TwainGetLastConditionCode());
}
_gdPictureImaging.TwainUnloadSourceManager(hwnd.ToInt32());
}
catch (Exception ex)
{
Outils.log.Error(ex.ToString(), ex);
throw ex;
}
}
public void m_BackgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
try
{
int imageId = System.Convert.ToInt32(e.UserState);
string imageNo = e.ProgressPercentage.ToString();
Thread t = new Thread(() =>
{
using (GdPictureImaging gdpictureImaging = new GdPictureImaging())
{
string FileName = m_strCheminDocumentScanner + imageNo + m_extension;
gdpictureImaging.AutoDeskew(imageId);
gdpictureImaging.CropBlackBorders(imageId);
gdpictureImaging.FxEdgeEnhance(imageId);
GdPictureStatus status = gdpictureImaging.SaveAsPDF(imageId, FileName, false, "test", "test", "test", "test", "test");
if (status != GdPictureStatus.OK)
{
MessageBox.Show("Erreur en écrivant le fichier " + FileName + ". Status: " + status);
}
gdpictureImaging.ReleaseGdPictureImage(imageId);
}
}
);
t.Start();
}
catch (Exception ex)
{
Outils.log.Error(ex.ToString(), ex);
throw ex;
}
}
When the second _backGroundWorker.RunWorkerAsync() occur a message box popup and the scanned document isn't wrote on the pdf destination folder.
the message :
McVrsServer.McVrsServerMutexMgr(): The Wait completed due to an anbondonned mutex.
My Gdpicture library is on version 14.1.0.20
My VRS is VRS elite 5.1.1
My Framework c# is 4.6
Best regards
Recently, our Document Scanning departement request us to be able to add new page to a box in a C# Tools.
So the process is to scan by asynchronous background worker a batch of documents, work on it and add some other document in a second time.
The C# Tools use the recommended way of Doing things.
private BackgroundWorker _backGroundWorker = new System.ComponentModel.BackgroundWorker();
_backGroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(m_BackgroundWorker_DoWork);
_backGroundWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(m_BackgroundWorker_ProgressChanged);
public void m_BackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
try
{
IntPtr hwnd = IntPtr.Zero;
if (_gdPictureImaging.TwainOpenDefaultSource(hwnd.ToInt32()))
{
int scanCount = 0;
_gdPictureImaging.TwainSetHideUI(scanProperty.chkHideGUI.Checked);
_gdPictureImaging.TwainSetModalUI(scanProperty.chkModalUI.Checked);
_gdPictureImaging.TwainSetIndicators(true);
if (_gdPictureImaging.TwainIsFeederSelected())
{
_gdPictureImaging.TwainSetAutoFeed(true);
_gdPictureImaging.TwainSetAutoScan(true);
}
do
{
if (!_backGroundWorker.CancellationPending)
{
int imageId = _gdPictureImaging.TwainAcquireToGdPictureImage(hwnd.ToInt32());
if (imageId != 0)
{
scanCount++;
_backGroundWorker.ReportProgress(scanCount, imageId);
}
}
else
{
break;
}
} while (_gdPictureImaging.TwainGetState() > TwainStatus.TWAIN_SOURCE_ENABLED);
if (scanCount > 0)
{
Outils.log.Info("Numérisation finie, " + scanCount + " page(s) acquired");
}
else
{
MessageBox.Show(this, "Le scan a échoué, veuillez consulter les logs.");
}
_gdPictureImaging.TwainCloseSource();
}
else
{
MessageBox.Show("Unable to open the default TWAIN source. " + '\r' + "Result code: " + _gdPictureImaging.TwainGetLastResultCode() +
'\r' + "Condition code: " + _gdPictureImaging.TwainGetLastConditionCode());
}
_gdPictureImaging.TwainUnloadSourceManager(hwnd.ToInt32());
}
catch (Exception ex)
{
Outils.log.Error(ex.ToString(), ex);
throw ex;
}
}
public void m_BackgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
try
{
int imageId = System.Convert.ToInt32(e.UserState);
string imageNo = e.ProgressPercentage.ToString();
Thread t = new Thread(() =>
{
using (GdPictureImaging gdpictureImaging = new GdPictureImaging())
{
string FileName = m_strCheminDocumentScanner + imageNo + m_extension;
gdpictureImaging.AutoDeskew(imageId);
gdpictureImaging.CropBlackBorders(imageId);
gdpictureImaging.FxEdgeEnhance(imageId);
GdPictureStatus status = gdpictureImaging.SaveAsPDF(imageId, FileName, false, "test", "test", "test", "test", "test");
if (status != GdPictureStatus.OK)
{
MessageBox.Show("Erreur en écrivant le fichier " + FileName + ". Status: " + status);
}
gdpictureImaging.ReleaseGdPictureImage(imageId);
}
}
);
t.Start();
}
catch (Exception ex)
{
Outils.log.Error(ex.ToString(), ex);
throw ex;
}
}
When the second _backGroundWorker.RunWorkerAsync() occur a message box popup and the scanned document isn't wrote on the pdf destination folder.
the message :
McVrsServer.McVrsServerMutexMgr(): The Wait completed due to an anbondonned mutex.
My Gdpicture library is on version 14.1.0.20
My VRS is VRS elite 5.1.1
My Framework c# is 4.6
Best regards