Hi,
I call this method from differents threads, but it seems as if they execute sequentially instead of parallel.
public void generate(string fromPath, string toPath)
{
Aspose.Words.Document doc = new Aspose.Words.Document(fromPath);
doc.Save(toPath);
}
Is there any solution to run that code parallel?
Thx
Hi
Thanks for your request. You cannot use document object in multiple threads. Aspose.Words is multithread safe as long as only one thread works on a document at a time. It is a typical scenario to have one thread working on one document. Different threads can safely work on different documents at the same time.
Best regards,
Thanks for fast reply. I called that generate method with different “fromPath”, so in theory they were different documents.
Hi
Thanks for your inquiry. Please try using the following code:
DateTime start = DateTime.Now;
// Get file names of document, which should be converted.
string[] docNames = Directory.GetFiles("C:\\Temp\\", "*.docx");
List <Thread> threads = new List <Thread> ();
// Convert all documents.
foreach(string name in docNames)
{
Document doc = new Document(name);
ConvertHelper helper = new ConvertHelper(doc, name + ".pdf");
Thread thread = new Thread(helper.Convert);
threads.Add(thread);
thread.Start();
}
Console.WriteLine("Converting: {0} seconds", (DateTime.Now - start).TotalSeconds);
// Wait until everything's finished.
foreach(Thread thread in threads)
thread.Join();
Console.WriteLine("Finished: {0} seconds", (DateTime.Now - start).TotalSeconds);
private class ConvertHelper
{
public ConvertHelper(Document document, string toPath)
{
mDocument = document;
mToPath = toPath;
}
public void Convert()
{
Console.WriteLine("start converting");
mDocument.Save(mToPath, SaveFormat.Pdf);
Console.WriteLine("end converting");
}
private readonly Document mDocument;
private readonly string mToPath;
}
Best regards,