Detenga la conversión o la carga con InterruptMonitor cuando tarde demasiado
Posibles escenarios de uso
Aspose.Diagram allows you to stop the conversion of Diagram to various formats like PDF, HTML etc. using the InterrumpirMonitor objeto cuando está tardando demasiado. El proceso de conversión suele hacer un uso intensivo de la CPU y la memoria y suele ser útil detenerlo cuando los recursos son limitados. Puedes usarInterrumpirMonitor tanto para detener la conversión como para detener la carga enorme diagram. UtiliceDiagram.InterruptMonitor propiedad para detener la conversión yLoadOptions.InterruptMonitor propiedad para carga enorme diagram.
Detenga la conversión o la carga con InterruptMonitor cuando tarde demasiado
El siguiente código de ejemplo explica el uso deInterrumpirMonitor object. The code converts quite a large Visio file to PDF. It will take several seconds (i.e. más de 30 segundos) para convertirlo debido a estas líneas de código.
Aspose.Diagram.LoadOptions o = new Aspose.Diagram.LoadOptions(LoadFileFormat.VSDX);
o.InterruptMonitor = im;
Diagram diagram = new Diagram("Huge.vsdx", o);
Como vesEnorme.vsdx es un archivo VSDX bastante grande. sin embargo, el**StopConversionOrLoadingUsingInterruptMonitor()**El método interrumpe la conversión después de 10 segundos y el programa termina/finaliza. Utilice el siguiente código para ejecutar el código de muestra.
new StopConversionOrLoadingUsingInterruptMonitor().TestRun();
Código de muestra
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."); | |
} | |
} |