Stop conversion or loading using InterruptMonitor when it is taking too long
Possible Usage Scenarios
Aspose.Diagram allows you to stop the conversion of Diagram to various formats like PDF, HTML etc. using the InterruptMonitor object when it is taking too long. The conversion process is often both CPU and Memory intensive and it is often useful to halt it when resources are limited. You can use InterruptMonitor both for stopping conversion as well as to stop loading huge diagram. Please use Diagram.InterruptMonitor property for stopping conversion and LoadOptions.InterruptMonitor property for loading huge diagram.
Stop conversion or loading using InterruptMonitor when it is taking too long
The following sample code explains the usage of InterruptMonitor object. The code converts quite a large Visio file to PDF. It will take several seconds (i.e. more than 30 seconds) to get it converted because of these lines of code.
Aspose.Diagram.LoadOptions o = new Aspose.Diagram.LoadOptions(LoadFileFormat.VSDX);
o.InterruptMonitor = im;
Diagram diagram = new Diagram("Huge.vsdx", o);
As you see Huge.vsdx is quite a huge VSDX file. However, the StopConversionOrLoadingUsingInterruptMonitor() method interrupts the conversion after 10 seconds and program ends/terminates. Please use the following code to execute the sample code.
new StopConversionOrLoadingUsingInterruptMonitor().TestRun();
Sample Code
import com.aspose.diagram.Diagram; | |
import com.aspose.diagram.InterruptMonitor; | |
import com.aspose.diagram.SaveFileFormat; | |
public class StopConversionOrLoadingUsingInterruptMonitor | |
{ | |
//Create InterruptMonitor object | |
InterruptMonitor im = new InterruptMonitor(); | |
public class ThreadStart extends Thread | |
{ | |
private int ThreadFunc; | |
public ThreadStart(int threadFunc) | |
{ | |
this.ThreadFunc = threadFunc; | |
} | |
//This function will create diagram and convert it to Pdf format | |
void CreateDiagramAndConvertItToPdfFormat() throws Exception | |
{ | |
//Create a diagram object | |
Diagram diagram = new Diagram("huge.vsdx"); | |
//Assign it InterruptMonitor object | |
diagram.setInterruptMonitor(im); | |
try | |
{ | |
//Save the workbook to Pdf format | |
diagram.save( "output_InterruptMonitor.pdf",SaveFileFormat.PDF); | |
//Show successful message | |
System.out.println("Diagram to PDF - Successful Conversion"); | |
} | |
catch (Exception ex) | |
{ | |
System.out.println("Diagram process Interrupted - Message: " + ex.getMessage()); | |
} | |
} | |
//This function will interrupt the conversion process after 10s | |
void WaitForWhileAndThenInterrupt() throws Exception | |
{ | |
Thread.sleep(1000 * 10); | |
im.interrupt(); | |
} | |
public void run() | |
{ | |
try | |
{ | |
if(this.ThreadFunc == 1) | |
{ | |
CreateDiagramAndConvertItToPdfFormat(); | |
} | |
if(this.ThreadFunc == 2) | |
{ | |
WaitForWhileAndThenInterrupt(); | |
} | |
} | |
catch(Exception ex) | |
{ | |
System.out.println("Process Interrupted - Message: " + ex.getMessage()); | |
} | |
} | |
}//ThreadStart | |
public void TestRun() throws Exception | |
{ | |
ThreadStart t1 = new ThreadStart(1); | |
ThreadStart t2 = new ThreadStart(2); | |
t1.start(); | |
t2.start(); | |
t1.join(); | |
t2.join(); | |
} | |
public static void main(String[] args) throws Exception { | |
new StopConversionOrLoadingUsingInterruptMonitor().TestRun(); | |
// Print the message | |
System.out.println("StopConversionOrLoadingUsingInterruptMonitor executed successfully."); | |
} | |
} |