[英]Executing again after the task is done
我要在完成30份打印(在打印機中逐字打印)后暫停約10分鍾,然后再次執行。
public class printImage {
static public void main(String args[]) throws Exception {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(30));
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
PrintService ps = pss[4];
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
FileInputStream fin = new FileInputStream("mypic.JPG");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
job.print(doc, pras);
fin.close();
}
}
看來您需要檢查PrintJobListener接口。
此偵聽器接口的實現應附加到DocPrintJob上,以監視打印機作業的狀態。 這些回調方法可以在處理打印作業的線程或服務創建的通知線程上調用。 無論哪種情況,客戶端都不應該在這些回調中執行冗長的處理。
您可以將代碼修改為:
public static void main(String[] args) throws IOException, PrintException {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(30));
PrintService pss[] = (PrintService[]) PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
PrintService ps = pss[4];
System.out.println("Printing to " + ps);
DocPrintJob job = pss[0].createPrintJob();
FileInputStream fin = new FileInputStream("mypic.JPG");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
// Monitor print job events
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, pras);
// Wait for the print job to be done
pjDone.waitForDone();
fin.close();
}
PrintJobWatcher
類為:
class PrintJobWatcher {
// true iff it is safe to close the print job's input stream
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
完成打印工作后,您可以在此處進行睡眠調用,而不是allDone
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.