C# Code for Interruption Token | Manually Interrupt Aspose.Words while Page Layout Model is Built | Word to PDF Conversion | Thread Start Stop

Hello Support,


We have found the following set of code to avoid memory leak :

///

/// My Aspose document.

///

public class MyDocument : Aspose.Words.Document

{

public MyDocument(string fileName) : base(fileName) { }

public bool TryToSave(string fileName, int timeout)

{

InterruptionToken token = new InterruptionToken();

bool finished = SaveWithTimeout(token,

() =>

{

token.BindToCurrentThread();

try

{

Save(fileName);

}

catch (Exception ex)

{

Console.WriteLine(Interrupted);

}

}, timeout);

return finished;

}

private bool SaveWithTimeout(InterruptionToken token, ThreadStart threadStart, int timeout)

{

Thread workerThread = new Thread(threadStart);

workerThread.Start();

bool finished = workerThread.Join(timeout);

if (!finished)

{

token.Interrupt();

}

return finished;

}

}

MyDocument myDoc = new MyDocument(MyDir + “in.docx”);

bool done = myDoc.TryToSave(MyDir + “Out.pdf”, 1000);

Console.WriteLine(done ? “Converted” : “Interrupted by timeout.”);



But before the calling the “Save” method we have use two lines of code to replace Mail Merge Fields:

MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveContainingFields | MailMergeCleanupOptions.RemoveUnusedRegions;

MailMerge.Execute(fieldNames, fieldValues);


The PDF file is generated but mail merge fields are not replaced. We are calling “TryToSave” method from web page. In this method we have also passed two additional parameters for merge fields.


Please let me know what is the solution.




Hi Somnath,


Thanks for your inquiry.
Somnath:
Hello Support,

We have found the following set of code to avoid memory leak :

///

/// My Aspose document.

///

public class MyDocument : Aspose.Words.Document

{

public MyDocument(string fileName) : base(fileName) { }

public bool TryToSave(string fileName, int timeout)

{

InterruptionToken token = new InterruptionToken();

bool finished = SaveWithTimeout(token,

() =>

{

token.BindToCurrentThread();

try

{

Save(fileName);

}

catch (Exception ex)

{

Console.WriteLine(Interrupted);

}

}, timeout);

return finished;

}

private bool SaveWithTimeout(InterruptionToken token, ThreadStart threadStart, int timeout)

{

Thread workerThread = new Thread(threadStart);

workerThread.Start();

bool finished = workerThread.Join(timeout);

if (!finished)

{

token.Interrupt();

}

return finished;

}

}

MyDocument myDoc = new MyDocument(MyDir + “in.docx”);

bool done = myDoc.TryToSave(MyDir + “Out.pdf”, 1000);

Console.WriteLine(done ? “Converted” : “Interrupted by timeout.”);



But before the calling the “Save” method we have use two lines of code to replace Mail Merge Fields:

MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveContainingFields | MailMergeCleanupOptions.RemoveUnusedRegions;

MailMerge.Execute(fieldNames, fieldValues);


The PDF file is generated but mail merge fields are not replaced. We are calling “TryToSave” method from web page. In this method we have also passed two additional parameters for merge fields.

After an initial test with Aspose.Words for .NET 16.1.0, we were unable to reproduce above issue on our end. We would suggest you please upgrade to the latest version of Aspose.Words. You can download it from the following link. Hope, this helps.

Download latest version of Aspose.Words for .NET

You may also try slightly increasing timeout value. In case the problem still remains, please attach the following resources here for testing:

  • Your input Word document
  • Please create a standalone console/web application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start further investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

Hello Support,

We have tested the code with latest Aspose Words dll and it works with Merge Fields in our production site.

We want to know what should be the ideal Timeout Value. Because it might effect the performance of the application if we start creating threads with large Timeout Values.

private bool SaveWithTimeout(InterruptionToken token, ThreadStart threadStart, int timeout)
{
Thread workerThread = new Thread(threadStart);
workerThread.Start();

bool finished = workerThread.Join(timeout);
if (!finished)
{
token.Interrupt();
}
return finished;
}

FYI: The Current Timeout value we have set is : 600000 = 10 mins.

Let me know if it is safe to use this code in production website. We can see timeout errors in the page where we have used this code.

Please let me know urgently as it is affecting our site.

Thanks,
Somnath



Hello Support,

Please give me an update on this asap as we are having trouble in our production site. Also let me know if we should use the code for Memory Leakage.

Thanks,
Somnath

Hi Somnath,

Thanks for your inquiry. The processing time and memory usage fully depend on your documents and their complexity; therefore, we won’t be able to suggest one particular timeout value that might be sufficient for every document you have on your end. Many customers are using Aspose.Words on production environments. Ideally, Aspose.Words should not cause any memory leakages; however, in case you have any Word document(s) for which latest version of Aspose.Words throws memory problem, please do attach them here for testing. We will investigate the issue on our end and provide you more information.

Best regards,