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..