Class InterruptMonitor

Class InterruptMonitor

Το όνομα: Aspose.Imaging.Multithreading Συγκέντρωση: Aspose.Imaging.dll (25.4.0)

Πληροφορίες για τη διακοπή.

public class InterruptMonitor : IInterruptMonitor

Inheritance

object InterruptMonitor

Implements

IInterruptMonitor

Κληρονομημένα μέλη

object.GetType() , object.MemberwiseClone() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()

Examples

Το παρακάτω παράδειγμα δείχνει πώς να εκτελέσετε τη μετατροπή εικόνας σε ένα αφιερωμένο thread και να διακόψετε τη διαδικασία μέσα σε λίγα δευτερόλεπτα μετά την έναρξη.

/// <summary>
                                                                                                                                                           /// This is helper class which initiates image conversion and waits for its interruption.
                                                                                                                                                           /// </summary>
                                                                                                                                                           private class Worker
                                                                                                                                                           {
                                                                                                                                                               /// <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 save options.
                                                                                                                                                               /// </summary>
                                                                                                                                                               private readonly Aspose.Imaging.ImageOptionsBase saveOptions;

                                                                                                                                                               /// <summary>
                                                                                                                                                               /// The interrupt monitor.
                                                                                                                                                               /// </summary>
                                                                                                                                                               private readonly Aspose.Imaging.Multithreading.InterruptMonitor monitor;

                                                                                                                                                               /// <summary>
                                                                                                                                                               /// Initializes a new instance of the <see cref="Worker"></see> class.
                                                                                                                                                               /// </summary>
                                                                                                                                                               /// <param name="inputPath"/>The path to the input image.
                                                                                                                                                               /// <param name="outputPath"/>The path to the output image.
                                                                                                                                                               /// <param name="saveOptions"/>The save options.
                                                                                                                                                               /// <param name="monitor"/>The interrupt monitor.
                                                                                                                                                               public Worker(string inputPath, string outputPath, Aspose.Imaging.ImageOptionsBase saveOptions, Aspose.Imaging.Multithreading.InterruptMonitor monitor)
                                                                                                                                                               {
                                                                                                                                                                   this.inputPath = inputPath;
                                                                                                                                                                   this.outputPath = outputPath;
                                                                                                                                                                   this.saveOptions = saveOptions;
                                                                                                                                                                   this.monitor = monitor;
                                                                                                                                                               }

                                                                                                                                                               /// <summary>
                                                                                                                                                               /// Converts an image from one format to another. Handles interruption.
                                                                                                                                                               /// </summary>
                                                                                                                                                               public void ThreadProc()
                                                                                                                                                               {
                                                                                                                                                                   try
                                                                                                                                                                   {
                                                                                                                                                                       Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(this.inputPath);

                                                                                                                                                                       // Set a thread-local instance of the interrupt monitor.
                                                                                                                                                                       Aspose.Imaging.Multithreading.InterruptMonitor.ThreadLocalInstance = this.monitor;

                                                                                                                                                                       try
                                                                                                                                                                       {
                                                                                                                                                                           image.Save(this.outputPath, this.saveOptions);
                                                                                                                                                                       }
                                                                                                                                                                       catch (Aspose.Imaging.CoreExceptions.OperationInterruptedException e)
                                                                                                                                                                       {
                                                                                                                                                                           System.Console.WriteLine(
                                                                                                                                                                               "The worker thread #{0} has been interrupted at {1}",
                                                                                                                                                                               System.Threading.Thread.CurrentThread.ManagedThreadId,
                                                                                                                                                                               System.DateTime.Now);
                                                                                                                                                                       }
                                                                                                                                                                       finally
                                                                                                                                                                       {
                                                                                                                                                                           image.Dispose();

                                                                                                                                                                           // Reset the thread-local instance of the interrupt monitor.
                                                                                                                                                                           Aspose.Imaging.Multithreading.InterruptMonitor.ThreadLocalInstance = null;
                                                                                                                                                                       }
                                                                                                                                                                   }
                                                                                                                                                                   catch (System.Exception e)
                                                                                                                                                                   {
                                                                                                                                                                       // Print detailed information about any unexpected exception.
                                                                                                                                                                       System.Console.WriteLine(e);
                                                                                                                                                                   }
                                                                                                                                                               }
                                                                                                                                                           }

                                                                                                                                                           // Here is the main example using the Worker class.
                                                                                                                                                           string baseDir = "c:\\temp\\";

                                                                                                                                                           Aspose.Imaging.Multithreading.InterruptMonitor monitor = new Aspose.Imaging.Multithreading.InterruptMonitor();
                                                                                                                                                           Worker worker = new Worker(baseDir + "big.png", baseDir + "big.bmp", new Aspose.Imaging.ImageOptions.BmpOptions(), monitor);

                                                                                                                                                           // Start the worker in a dedicated thread.
                                                                                                                                                           System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(worker.ThreadProc));
                                                                                                                                                           thread.Start();

                                                                                                                                                           // Do some meaningful work here
                                                                                                                                                           System.Threading.Thread.Sleep(2000);

                                                                                                                                                           // Request to interrupt the worker thread
                                                                                                                                                           monitor.Interrupt();
                                                                                                                                                           System.Console.WriteLine("Interrupting the worker thread #{0} at {1}", thread.ManagedThreadId, System.DateTime.Now);

                                                                                                                                                           // Wait for interruption.
                                                                                                                                                           thread.Join();

                                                                                                                                                           System.Console.WriteLine("Done. Press ENTER to exit.");
                                                                                                                                                           System.Console.ReadLine();

                                                                                                                                                           // The output may look like this:
                                                                                                                                                           // Interrupting the worker thread #14 at 8/6/2019 3:57:53 PM
                                                                                                                                                           // The worker thread #14 has been interrupted at 8/6/2019 3:58:09 PM
                                                                                                                                                           // Done. Press ENTER to exit.

Constructors

InterruptMonitor()

public InterruptMonitor()

Properties

IsInterrupted

Αποκτά την τιμή που υποδεικνύει εάν οι πράξεις θα πρέπει να διακοπεί.

public virtual bool IsInterrupted { get; }

Αξία ιδιοκτησίας

bool

ThreadLocalInstance

Αποκτά ή ρυθμίζει την περίπτωση IInterruptMonitor η οποία είναι μοναδική για κάθε thread.

public static IInterruptMonitor ThreadLocalInstance { get; set; }

Αξία ιδιοκτησίας

IInterruptMonitor

Methods

Interrupt()

Στείλτε αίτηση για διακοπή των δραστηριοτήτων.

public virtual void Interrupt()

Examples

Το παρακάτω παράδειγμα δείχνει πώς να εκτελέσετε τη μετατροπή εικόνας σε ένα αφιερωμένο thread και να διακόψετε τη διαδικασία μέσα σε λίγα δευτερόλεπτα μετά την έναρξη.

/// <summary>
                                                                                                                                                           /// This is helper class which initiates image conversion and waits for its interruption.
                                                                                                                                                           /// </summary>
                                                                                                                                                           private class Worker
                                                                                                                                                           {
                                                                                                                                                               /// <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 save options.
                                                                                                                                                               /// </summary>
                                                                                                                                                               private readonly Aspose.Imaging.ImageOptionsBase saveOptions;

                                                                                                                                                               /// <summary>
                                                                                                                                                               /// The interrupt monitor.
                                                                                                                                                               /// </summary>
                                                                                                                                                               private readonly Aspose.Imaging.Multithreading.InterruptMonitor monitor;

                                                                                                                                                               /// <summary>
                                                                                                                                                               /// Initializes a new instance of the <see cref="Worker"></see> class.
                                                                                                                                                               /// </summary>
                                                                                                                                                               /// <param name="inputPath"/>The path to the input image.
                                                                                                                                                               /// <param name="outputPath"/>The path to the output image.
                                                                                                                                                               /// <param name="saveOptions"/>The save options.
                                                                                                                                                               /// <param name="monitor"/>The interrupt monitor.
                                                                                                                                                               public Worker(string inputPath, string outputPath, Aspose.Imaging.ImageOptionsBase saveOptions, Aspose.Imaging.Multithreading.InterruptMonitor monitor)
                                                                                                                                                               {
                                                                                                                                                                   this.inputPath = inputPath;
                                                                                                                                                                   this.outputPath = outputPath;
                                                                                                                                                                   this.saveOptions = saveOptions;
                                                                                                                                                                   this.monitor = monitor;
                                                                                                                                                               }

                                                                                                                                                               /// <summary>
                                                                                                                                                               /// Converts an image from one format to another. Handles interruption.
                                                                                                                                                               /// </summary>
                                                                                                                                                               public void ThreadProc()
                                                                                                                                                               {
                                                                                                                                                                   try
                                                                                                                                                                   {
                                                                                                                                                                       Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(this.inputPath);

                                                                                                                                                                       // Set a thread-local instance of the interrupt monitor.
                                                                                                                                                                       Aspose.Imaging.Multithreading.InterruptMonitor.ThreadLocalInstance = this.monitor;

                                                                                                                                                                       try
                                                                                                                                                                       {
                                                                                                                                                                           image.Save(this.outputPath, this.saveOptions);
                                                                                                                                                                       }
                                                                                                                                                                       catch (Aspose.Imaging.CoreExceptions.OperationInterruptedException e)
                                                                                                                                                                       {
                                                                                                                                                                           System.Console.WriteLine(
                                                                                                                                                                               "The worker thread #{0} has been interrupted at {1}",
                                                                                                                                                                               System.Threading.Thread.CurrentThread.ManagedThreadId,
                                                                                                                                                                               System.DateTime.Now);
                                                                                                                                                                       }
                                                                                                                                                                       finally
                                                                                                                                                                       {
                                                                                                                                                                           image.Dispose();

                                                                                                                                                                           // Reset the thread-local instance of the interrupt monitor.
                                                                                                                                                                           Aspose.Imaging.Multithreading.InterruptMonitor.ThreadLocalInstance = null;
                                                                                                                                                                       }
                                                                                                                                                                   }
                                                                                                                                                                   catch (System.Exception e)
                                                                                                                                                                   {
                                                                                                                                                                       // Print detailed information about any unexpected exception.
                                                                                                                                                                       System.Console.WriteLine(e);
                                                                                                                                                                   }
                                                                                                                                                               }
                                                                                                                                                           }

                                                                                                                                                           // Here is the main example using the Worker class.
                                                                                                                                                           string baseDir = "c:\\temp\\";

                                                                                                                                                           Aspose.Imaging.Multithreading.InterruptMonitor monitor = new Aspose.Imaging.Multithreading.InterruptMonitor();
                                                                                                                                                           Worker worker = new Worker(baseDir + "big.png", baseDir + "big.bmp", new Aspose.Imaging.ImageOptions.BmpOptions(), monitor);

                                                                                                                                                           // Start the worker in a dedicated thread.
                                                                                                                                                           System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(worker.ThreadProc));
                                                                                                                                                           thread.Start();

                                                                                                                                                           // Do some meaningful work here
                                                                                                                                                           System.Threading.Thread.Sleep(2000);

                                                                                                                                                           // Request to interrupt the worker thread
                                                                                                                                                           monitor.Interrupt();
                                                                                                                                                           System.Console.WriteLine("Interrupting the worker thread #{0} at {1}", thread.ManagedThreadId, System.DateTime.Now);

                                                                                                                                                           // Wait for interruption.
                                                                                                                                                           thread.Join();

                                                                                                                                                           System.Console.WriteLine("Done. Press ENTER to exit.");
                                                                                                                                                           System.Console.ReadLine();

                                                                                                                                                           // The output may look like this:
                                                                                                                                                           // Interrupting the worker thread #14 at 8/6/2019 3:57:53 PM
                                                                                                                                                           // The worker thread #14 has been interrupted at 8/6/2019 3:58:09 PM
                                                                                                                                                           // Done. Press ENTER to exit.
 Ελληνικά