amyn
amyn

Reputation: 942

Threadpool WaitAll 64 [Leadtools]

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?

Upvotes: 1

Views: 129

Answers (1)

usr
usr

Reputation: 171178

You waiting for those events does not cause the tasks to fail. More likely there is an exception which you do not notice (uncaught or swallowed).

Make your processing Task based and use Task.WaitAll. No limits there. You'll also get automatic error propagation.

Upvotes: 1

Related Questions