How to save tables asynchron

Hi, How can I write word tables to HTML asynchron.

I use following code:

foreach (Section section in WordDoc.Sections)
{
    NodeCollection tables = section.GetChildNodes(NodeType.Table, true);
    Parallel.ForEach(tables, table =>
    {
        string tablestructure = ((Table)table).ToString(SaveFormat.Html);
    });
}

But it runs in an exception: InvalidOperationException: “Invalid position of the enumerator” at ‘string tablestructure =…’

Can you help me ?

Kind regards,
@Nachti

@Nachti Aspose.Words is multithread safe as long as only one thread works on a document at a time. This is a typical scenario to have one thread working on one document. Different threads can safely work on different documents at the same time.
In your code multiple threads work with the same document. I am afraid, such scenario is not supported.

ok, I have document with 570 pages and 320 tables inside. I want to the tables as HTML but i takes 30 seconds per table on my server. How can I improve the performance ? More RAM, more CPU ? Any other advice ?

Thanks in advance
Kind regards,
Guido

@Nachti Could you please attach your input document here for testing? We will check the issue and provide you more information. 30 seconds per table it is really slow.

Sure:

Relatório_V1.zip (6.5 MB)

Kind regards,
Guido

@Nachti Thank you for additional information. It takes about 1 second to convert each table one by one to HTML on my side. I have used the following code for testing:

Document doc = new Document(@"C:\Temp\in.docx");

NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
Console.WriteLine(tables.Count);

foreach (Table t in tables)
{
    Stopwatch sw1 = new Stopwatch();
    sw1.Start();

    string html = t.ToString(SaveFormat.Html);

    sw1.Stop();
    Console.WriteLine(sw1.ElapsedMilliseconds / 1000d);
}

The whole process took 304 second on my side.

To speed up the process you can copy each table into a separate document and then convert each temporary document to HTML in parallel:

Document doc = new Document(@"C:\Temp\in.docx");

NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
// Copy each table into a separate document.
List<Document> tmpDocuments = new List<Document>();
foreach (Table t in tables)
{
    Document tmp = (Document)doc.Clone(false);
    tmp.EnsureMinimum();
    tmp.FirstSection.Body.PrependChild(tmp.ImportNode(t, true, ImportFormatMode.UseDestinationStyles));
    tmpDocuments.Add(tmp);
}
// Convert each tmp document to HTML in paralel.
Parallel.ForEach(tmpDocuments, tmp =>
{
    string tablestructure = tmp.ToString(SaveFormat.Html);
});