0

I am trying this code to convert a document using aspose word, I am trying to terminate the process of conversion of document when it takes too long (as it leads to memory leak and eats up all system resources), I never looked at threading or async before, but now as our production server is broken I am looking for a quick fix before digging any deeper,

This is what I tried but it kills the thread but keeps all the resources, I read some posts from What's wrong with using Thread.Abort() but I am not sure what is the best way to move forward, should I use Task and async but I don't know how to use them in this context,

  RunWithTimeout(() =>
  {
       status = AsposeConversion.ConvertToPDF(licensePath, fileName);
  }, TimeSpan.FromMilliseconds(1000 * 60 * 4));

 public static bool RunWithTimeout(ThreadStart threadStart, TimeSpan timeout)
 {
     Thread workerThread = new Thread(threadStart);
     workerThread.Start();

     bool finished = workerThread.Join(timeout);
     if (!finished)
         workerThread.Abort();

     return finished;
   }

Should I create a new process ? but then how can I timeout it or run conversion code line in it.

Edit

Sorry for confusion, I said it takes too long, but in real it never returns even for 4 hours I tested so far..

Community
  • 1
  • 1
Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • Hello! What do you exactly mean with "keeps all the resources"? – Yogster Dec 22 '14 at 13:15
  • @Jorge it takes all memory(RAM) space – Mathematics Dec 22 '14 at 13:19
  • And `AsposeConversion.ConvertToPDF` is a blocking method, right? – Yogster Dec 22 '14 at 13:20
  • I would assume "yes" – Mathematics Dec 22 '14 at 13:23
  • Create a new app domain with a wrapper function that calls the `AsposeConversion` assembly. If it takes too long, then tear down that app domain. That will kill the thread and also unload the Aspose assembly and release all the resources that it allocates. See [Application Domains](http://msdn.microsoft.com/en-us/library/ms173138%28v=vs.90%29.aspx) – Jim Mischel Dec 22 '14 at 15:22
  • 4 hours is abnormal, what is the size of Word document? Try to first save it to docx and then to pdf. The mysterious problem might get resolved when you save to docx. If not, please inform Aspose Support about it in forums, it should be fixed in the library. – Saqib Razzaq Jan 14 '15 at 17:06
  • Documents are dynamic Saqib, I already did let aspose team know few weeks ago, they said they replicated the issue and will fix in future, when i got no clue... – Mathematics Jan 15 '15 at 07:10

1 Answers1

1

Since AsposeConversion.ConvertToPDF is a blocking method, you cannot send a signal to tell the thread to end gracefully, as the thread will not check the signal until the blocking method has completed.

All I can suggest is to let the thread run until AsposeConversion.ConvertToPDF returns, then check within thread if the maximum allowed time has passed, and if so clean up before terminating.

Edit - After knowing the method call does not return at all

It looks like this is an issue with Aspose, so you should find out why that method call is never returning and see if there is something you are doing wrong, or any workaround if it is a bug.

If you don't manage to make the method work as expected, as an extreme solution you could try to run the code inside a Job object. It will allow you to execute your code as a separate process and limit the memory the process can use. You can then handle the OutOfMemoryException and clean up as necessary.

It is horrible, I know, but will prevent your server from crashing while you wait for a workaround / bug fix.

Community
  • 1
  • 1
Yogster
  • 884
  • 1
  • 7
  • 27
  • AsposeConversion.ConvertToPDF never returns... that's the issue here – Mathematics Dec 22 '14 at 13:24
  • Ok in that case you should make it clear in your question. "Taking too long" is not the same as "never returning" :-). As of your problem, it seems to be an issue with aspose, not multithreading, so I am afraid I cannot help. You will have to go find out why `AsposeConversion.ConvertToPDF` is behaving that way and see if there is any workaround for it. – Yogster Dec 22 '14 at 13:26
  • @CustomizedName, check out my edited answer. May the force be with you. – Yogster Dec 22 '14 at 13:39
  • This is one reason [Application Domains](http://msdn.microsoft.com/en-us/library/ms173138%28v=vs.90%29.aspx) were created. – Jim Mischel Dec 22 '14 at 15:24