I am aware of the fact that there is a limit to WaitAll ManualResetEvents i.e. 64. (any reason why 64?) Due to this limitation, my below code does not work when SourceFolders.Count > 64.
if (Source.SubFolders.Count > 0)
{
var threadPoolDoneEvents = new ManualResetEvent[Source.SubFolders.Count];
for (int i = 0; i < Source.SubFolders.Count; i++)
{
try
{
if (m_bRegisterCancelled == false)
{
threadPoolDoneEvents[i] = new ManualResetEvent(false);
AddSourceAgs variables = new AddSourceAgs();
variables.Source = Source;
variables.SubFolder = Source.SubFolders[i];
variables.DoneEvents = threadPoolDoneEvents[i];
ThreadPool.QueueUserWorkItem(AddSourceProcess, variables);
}
else
{
break;
}
}
catch (System.Exception Ex)
{
FireError(this, "AddSource", this.ExBuilder.Message(ERROR_CASE_DATABASE_ADD_SOURCE_EX), Ex);
}
}
WaitHandle.WaitAll(threadPoolDoneEvents);
}
I tried replacing the following line:
WaitHandle.WaitAll(threadPoolDoneEvents);
By this:
foreach (var waitHandle in threadPoolDoneEvents)
waitHandle.WaitOne();
By this causes something unexpected to occur - in my case, out of 100 files, randomly 20 - 30 files are processed completely and then rest are stopped in between. Any idea where my concepts are falling behind? Any solution to this problem? I have tried this but this also causes some random errors like processing each task twice or something like that.
UPDATE After debugging, I found out that the tasks failed due to an exception thrown on this line of code:
try
{
RasterImage image = m_codecs.Load(m_documentNameWithPath, pageNum);
m_codecs.Save(image, m_outputPath+ "\\\\" + pageNum.ToString("D4") + ".png", RasterImageFormat.Png, 0);
return true;
}
catch (Exception Ex)
{
m_conversionErrors.Add(Ex);
return false;
}
Following was the Exception thrown:
Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.
Any solution do this? And even explain what this is?