인터럽트 모니터 지원
Contents
[
Hide
]
변환을 위한 인터럽트 모니터 지원
이 문서에서는 Aspose.PSD를 사용하여 이미지 변환에 대한 인터럽트를 지원하는 방법에 대해 설명합니다. 이 예제는 변환 중에 인터럽트를 생성하는 데 Interrupt 클래스를 사용합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
ImageOptionsBase saveOptions = new PngOptions(); | |
InterruptMonitor monitor = new InterruptMonitor(); | |
string source = "big2.psb"; | |
string output = "big_out.png"; | |
SaveImageWorker worker = new SaveImageWorker(source, output, saveOptions, monitor); | |
Thread thread = new Thread(new ThreadStart(worker.ThreadProc)); | |
try | |
{ | |
thread.Start(); | |
// The timeout should be less than the time required for full image conversion (without interruption). | |
Thread.Sleep(3000); | |
// Interrupt the process | |
monitor.Interrupt(); | |
Console.WriteLine("Interrupting the save thread #{0} at {1}", thread.ManagedThreadId, System.DateTime.Now); | |
// Wait for interruption... | |
thread.Join(); | |
} | |
finally | |
{ | |
// Delete the output file. | |
if (File.Exists(output)) | |
{ | |
File.Delete(output); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
{ | |
/// <summary> | |
/// The path to the input image. | |
/// </summary> | |
private readonly string inputPath; | |
/// <summary> | |
/// The path to the output image. | |
/// </summary> | |
private readonly string outputPath; | |
/// <summary> | |
/// The interrupt monitor. | |
/// </summary> | |
private readonly InterruptMonitor monitor; | |
/// <summary> | |
/// The save options. | |
/// </summary> | |
private readonly ImageOptionsBase saveOptions; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="SaveImageWorker" /> class. | |
/// </summary> | |
/// <param name="inputPath">The path to the input image.</param> | |
/// <param name="outputPath">The path to the output image.</param> | |
/// <param name="saveOptions">The save options.</param> | |
/// <param name="monitor">The interrupt monitor.</param> | |
public SaveImageWorker(string inputPath, string outputPath, ImageOptionsBase saveOptions, InterruptMonitor monitor) | |
{ | |
this.inputPath = inputPath; | |
this.outputPath = outputPath; | |
this.saveOptions = saveOptions; | |
this.monitor = monitor; | |
} | |
/// <summary> | |
/// Tries to convert image from one format to another. Handles interruption. | |
/// </summary> | |
public void ThreadProc() | |
{ | |
using (Image image = Image.Load(this.inputPath)) | |
{ | |
InterruptMonitor.ThreadLocalInstance = this.monitor; | |
try | |
{ | |
image.Save(this.outputPath, this.saveOptions); | |
} | |
catch (OperationInterruptedException e) | |
{ | |
Console.WriteLine("The save thread #{0} finishes at {1}", Thread.CurrentThread.ManagedThreadId, DateTime.Now); | |
Console.WriteLine(e); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
} | |
finally | |
{ | |
InterruptMonitor.ThreadLocalInstance = null; | |
} | |
} | |
} | |
} |