简体   繁体   English

任务完成后再次执行

[英]Executing again after the task is done

I want to pause for about 10 mins after the printing of 30 copies (literally printing in printer) is done and execute it again. 我要在完成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();
    } 
}

It seems that you need to check PrintJobListener interface. 看来您需要检查PrintJobListener接口。

Implementations of this listener interface should be attached to a DocPrintJob to monitor the status of the printer job. 此侦听器接口的实现应附加到DocPrintJob上,以监视打印机作业的状态。 These callback methods may be invoked on the thread processing the print job, or a service created notification thread. 这些回调方法可以在处理打印作业的线程或服务创建的通知线程上调用。 In either case the client should not perform lengthy processing in these callbacks. 无论哪种情况,客户端都不应该在这些回调中执行冗长的处理。

You can modify your code as: 您可以将代码修改为:

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();

    }

The class PrintJobWatcher is: 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) {
        }
    }
}

When your printing job is done you can put a sleep call there rather than allDone method. 完成打印工作后,您可以在此处进行睡眠调用,而不是allDone方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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