繁体   English   中英

任务完成后再次执行

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM