簡體   English   中英

TimerTask 在午夜后停止運行

[英]TimerTask stops running after midnight

我有一小部分程序有一個定時器,每 15 分鍾通過命令行 (fswebcam) 使用 usb 網絡攝像頭拍照。

代碼是這樣的:

public static final String HOME_DIR = System.getProperty("user.home") + "/";
public static final String PGP_DIR = HOME_DIR + "PGP/";

public static final String COLLECTION_DATA_DIR = PGP_DIR + "collectionData/";
public static final String SENSOR_CALIBRATION_DATA = PGP_DIR + "sensorCalibration/";
public static final String PICTURE_DIR = PGP_DIR + "pictures/";
public static final String ALARM_DIR = PGP_DIR + "alarms/";
private class PictureTakerTask extends TimerTask{
        Timer t;
        public void start(){
            if(t != null){
                t.cancel();
                t.purge();
                t = null;
                this.cancel();
            }
            t = new Timer(true);
            t.scheduleAtFixedRate(this, 0, 1000 * 60 * 15); //takes a picture every 15 minutes
        }

        public void stop(){
            if(running) return;
            if(t != null){
                t.cancel();
                t.purge();
                t = null;
            }
            this.cancel();
        }

        @Override
        public void run() {
            
            String filename = getPictureFilename();
            if(filename == null) return;
            Process p;
            try {
                CommIO.printLog("Taking a picture");
                String file;
                light.setGreenPWM(80);
                p = Runtime.getRuntime().exec("fswebcam -r 1920x1080 --no-banner -S 1 " + filename);
                p.waitFor();
                System.out.println ("picture taken with exit value: " + p.exitValue());
                p.destroy();
                light.setGreenPWM(0);
            } catch (Exception e) {
                e.printStackTrace();
                light.setGreenPWM(0);
            }
        }

        private String getPictureFilename(){
            //make folder if it doesn't exist already
            SimpleDateFormat justDate = new SimpleDateFormat("MM-dd-yy");
            String date = justDate.format(new Date());
            String s = PICTURE_DIR + date + "/";
            File picDir = new File(s);
            if(!picDir.exists()){
                if(picDir.mkdir()){
                    Alert alert = new Alert(AlertType.ERROR, "Couldn't make picture directory: " + s, ButtonType.OK);
                }
            }
            //find an unused filename
            String picFileName = s + "plant_01.jpg";
            File tempFile = new File(picFileName);
            int i = 1;
            while (tempFile.exists()) { //finds the next nonexistent name for data spreadsheet
                String num = i < 10 ? "0" + i : "" + i;
                picFileName = s + "plant_" + num + ".jpg";
                i++;
                tempFile = new File(picFileName);
                if(i > 10000) break;
            }
            return picFileName;
        }
    }

該線程啟動並正常工作,直到午夜。 第二天會拍照失敗。 我一輩子都弄不明白為什么它會停止。 如果我停止並重新啟動任務,它將再次正常工作。 如果我什至通過程序外的終端拍照,它會再次正常工作。 它只會在第二天停止拍照。 它甚至會創建第二天的文件夾,只是沒有圖片。 沒有錯誤信息(我能找到)。

有人對此有任何想法或經驗嗎? 在你說之前,不,我不能使用 motion 或 Cron,因為我需要與 run() function 中的“light”object 同步。

回答我自己的問題。 簡單的錯誤,實際上:

if(picDir.mkdir()){
    Alert alert = new Alert(AlertType.ERROR, "Couldn't make picture directory: " + s, ButtonType.OK);
}

忘記了條件的否定。 我通常一天多次運行該程序以檢查其他更改,因此它第一次創建文件夾,使線程崩潰,並在接下來的幾次啟動程序時運行良好。 但是當第二天到來時,它發出警報並大發脾氣,因為它不在 FX 線程上。 很難注意到,因為我什至忘了顯示警報。

我喜歡 java-fx,但有時它會很挑剔。 通過添加否定並將警報放入 Platform.runLater Runnable 來修復。

您可以嘗試在晚上 11:59:59 將時間設置為凌晨 12:00:01。 我不確定這是否會解決它。 while 循環可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM